2011-05-28 72 views
0

我正在为tinyMCE添加一个按钮,并且我想知道如何使用javascript将标签内部的文本封装在标签中(例如,这种突出显示的文本被包装在[highlight][/highlight]标签中)。 并且现在整个tinymcejavascript wrap with text标签

(function() { 
tinymce.create('tinymce.plugins.shoutButton', { 
    init : function(ed, url) { 
     ed.addButton('shout.button', { 
      title : 'shout.button', 
      image : 'viral.gif', 
      onclick : function() { 
       window.alert("booh"); 
     }); 
    }, 
    createControl : function(n, cm) { 
     return null; 
    }, 
    getInfo : function() { 
     return { 
      longname : "Shout button", 
      author : 'SAFAD', 
      authorurl : 'http://safadsoft.com/', 
      infourl : 'http://safadsoft.com/', 
      version : "1.0" 
     }; 
    } 
}); 
tinymce.PluginManager.add('shout.button', tinymce.plugins.ShoutButton); 

})();

回答

1

的问题是语法错误,不能正常闭合支架和一些失踪分号,采用的帮助下,真棒Jsfiddle的JSHint和JSLint的我固定它:

(function() { 
    tinymce.create('tinymce.plugins.shoutButton', { 
     init: function (ed, url) { 
      ed.addButton('shout.button', { 
       title: 'shout.button', 
       image: 'viral.gif', 
       onclick: function() { 
        window.alert("booh"); 
       } 
      }); 
      createControl: function (n, cm) { 
       return null; 
      } 
      getInfo: function() { 
       return { 
        longname: "Shout button", 
        author: 'You !', 
        authorurl: 'http://example.com/', 
        infourl: 'http://example.com/', 
        version: "1.0" 
       }; 
      } 
     } 
    }); 
    tinymce.PluginManager.add('shout.button', tinymce.plugins.ShoutButton); 
})(); 

顺祝商祺

+2

我投下了你的答案,因为你没有发布实现最终目标的最终代码。 – Webnet 2012-05-01 19:16:39

+0

同意,为此我编辑了答案,接受我真诚的道歉! – SAFAD 2013-03-05 10:51:20

2

您可以使用setSelectionRange(mozilla/webkit)或selection.createRange(IE)方法在textarea中查找当前突出显示的文本。 我在jsfiddle上举了一个例子,但已经注释掉了你的正则表达式,因为它在很多情况下挂起了浏览器。你需要使它更具限制性,而且它目前还会传递比youtube url更多的其他内容。

但是,该示例提供了一个工作解决方案,用于获取当前选定的文本,您可以在修复模式后应用于idPattern.exec()。

idPattern = /(?:(?:[^v]+)+v.)?([^&=]{11})(?=&|$)/; 
    // var vidId = prompt("YouTube Video", "Enter the id or url for your video"); 


    var vidId; 
     el = document.getElementById('texty'); 
     if (el.setSelectionRange) { 
     var vidId = el.value.substring(el.selectionStart,el.selectionEnd); 
     } 
     else if(document.selection.createRange()) { 
      var vidId = document.selection.createRange().text; 
     } 

    alert(vidId); 

编辑:包装突出显示的文本并将其输出回元素。 example

el = document.getElementById('texty'); 
    if (el.setSelectionRange) { 

    el.value = el.value.substring(0,el.selectionStart) + "[highlight]" + el.value.substring(el.selectionStart,el.selectionEnd) + "[/highlight]" + el.value.substring(el.selectionEnd,el.value.length); 

    } 
    else if(document.selection.createRange()) { 
     document.selection.createRange().text = "[highlight]" + document.selection.createRange().text + "[/highlight]"; 
    } 
+0

我说这是我在制作按钮到tinyMCE时发现的一些教程中的一个例子,我想要的只是用高亮标记包装文本 – SAFAD 2011-05-28 22:32:28

+0

那么这正是它所做的。它会找到突出显示的文本供您随时随地使用。 – Niklas 2011-05-28 23:19:20

+0

现在找到它后,如何将它包装在那些[highlight] [/ highlight]标签下,我很坏@ JS – SAFAD 2011-05-28 23:34:27