2012-09-21 166 views
0

我正在尝试在我的网站中添加搜索功能。其中我想在tinymce编辑器中搜索内容,并在找到匹配项时将它们选中。在tinymce编辑器中选择文本

我怎样才能使选定的文本是否匹配。如果有任何人知道,请回复 enter image description here

+0

如果搜索是在一个单一的页面,然后在浏览器的内置_寻找page_中的功能是最简单的解决方案 – andyb

回答

-1

这也不是那么困难。最简单的方法是查看searchreplace插件的searchNext函数(我将在下面添加代码)。

插件代码在tinymce弹出窗口中被执行,所以您需要稍微调整它。这里有您需要事先设置,使功能工作varibales:

var ed = tinymce.get('your_editor_id'); 
var se = ed.selection; 
var r = se.getRng(); 
// var m = this.lastMode; // not needed here 
var fl = 0; 
var w = ed.getWin(); 
var wm = ed.windowManager; 
var fo = 0; 
//var f = document.forms[0]; //not needed here (plugin relevant) 
var s = 'word_to_be_searched_for'; 
var b = 0; // direction of the search (forward=0, backward = 1) 
var ca = 0; // casesensitiviy of the search (not casesensitive = 0, casesensitive = 1) 
var rs = ''; // possibility to use a string to replace found text 

下面是函数:

searchNext : function(a) { 
    var ed = tinyMCEPopup.editor, se = ed.selection, r = se.getRng(), f, m = this.lastMode, s, b, fl = 0, w = ed.getWin(), wm = ed.windowManager, fo = 0, err = 0; 

    // Get input 
    f = document.forms[0]; 
    s = f[m + '_panel_searchstring'].value; 
    b = f[m + '_panel_backwardsu'].checked; 
    ca = f[m + '_panel_casesensitivebox'].checked; 
    rs = f['replace_panel_replacestring'].value; 

    if (tinymce.isIE) { 
     r = ed.getDoc().selection.createRange(); 
    } 

    if (s == '') 
     return; 

    function fix() { 
     // Correct Firefox graphics glitches 
     // TODO: Verify if this is actually needed any more, maybe it was for very old FF versions? 
     r = se.getRng().cloneRange(); 
     ed.getDoc().execCommand('SelectAll', false, null); 
     se.setRng(r); 
    }; 

    function replace() { 
     ed.selection.setContent(rs); // Needs to be duplicated due to selection bug in IE 
    }; 

    // IE flags 
    if (ca) 
     fl = fl | 4; 

    switch (a) { 
     case 'all': 
      // Move caret to beginning of text 
      ed.execCommand('SelectAll'); 
      ed.selection.collapse(true); 

      if (tinymce.isIE) { 
       ed.focus(); 
       r = ed.getDoc().selection.createRange(); 

       while (r.findText(s, b ? 1 : 1, fl)) { 
        r.scrollIntoView(); 
        r.select(); 
        replace(); 
        fo = 1; 

        if (b) { 
         r.moveEnd("character", -(rs.length)); // Otherwise will loop forever 
        } 
       } 

       tinyMCEPopup.storeSelection(); 
      } else { 
       while (w.find(s, ca, b ? 0 : 0, false, false, false, false)) { 
        replace(); 
        fo = 1; 
       } 
      } 

      if (fo) 
       tinyMCEPopup.alert(ed.getLang('searchreplace_dlg.allreplaced')); 
      else 
       tinyMCEPopup.alert(ed.getLang('searchreplace_dlg.notfound')); 

      return; 

     case 'current': 
      if (!ed.selection.isCollapsed()) 
       replace(); 

      break; 
    } 

    se.collapse(b); 
    r = se.getRng(); 

    // Whats the point 
    if (!s) 
     return; 

    if (tinymce.isIE) { 
     ed.focus(); 
     r = ed.getDoc().selection.createRange(); 

     if (r.findText(s, b ? -1 : 1, fl)) { 
      r.scrollIntoView(); 
      r.select(); 
     } else 
     { 
      err = 1; 
      tinyMCEPopup.alert(ed.getLang('searchreplace_dlg.notfound')); 
     } 
     tinyMCEPopup.storeSelection(); 
    } else { 
     if (!w.find(s, ca, b, false, false, false, false)) 
     { 
      err = 1; 
      tinyMCEPopup.alert(ed.getLang('searchreplace_dlg.notfound')); 
     } 
     else 
      fix(); 
    } 

    // Focus input field if search yieled no error 
    if (!err && !tinymce.isIE) { 
     f[m + '_panel_searchstring'].focus(); 
    } 
} 
+0

我使用上面的代码,并更改ed变量中的编辑器id和设置搜索词在s变量,但我不能让他们选择下面是代码''function searchnotes(){ var snote = $('#txt_name')。val(); var ed = tinymce.get(“txtnotes”); var se = ed.selection; var r = se.getRng(); // setRng(r); var fl = 0; var w = ed.getWin(); var wm = ed.windowManager; var fo = 0; var s = snote; alert(r); var b = 0; var ca = 0; var rs =''; } – rohit

+0

那么你将需要调试代码行的代码行 – Thariama

+0

我投下了它,因为它是不可读的,你可以使用适当的变量名?很难看到这里发生了什么 – Isaac