简体   繁体   English

加 标签到textarea中的选定文本

[英]Add <b></b> tags to selected text in a textarea

How to create some javascript to do the following - when the user clicks the "BOLD" button, the selected text gets a "<b> " and a " </b>" wrapped around it. 如何创建一些javascript来执行以下操作 - 当用户单击“BOLD”按钮时,所选文本会包含“<b>”和“</ b>”。

 <form name="my">
<textarea name="textarea"></textarea><br />
<input type="button" value="bold" onclick="formatText ('b');" />
<input type="button" value="italic" onclick="formatText ('i');" />
<input type="button" value="underline" onclick="formatText ('u');" />
</form> 

please help me to create java-script for supporting almost web browser 请帮我创建支持几乎Web浏览器的java脚本

You should use an existing rich text editor, they are configurable so you can give users only the features what you want them to use. 您应该使用现有的富文本编辑器,它们是可配置的,因此您只能为用户提供您希望它们使用的功能。 They also have the added benefit of working across a range of different browsers. 它们还具有在各种不同浏览器中工作的额外好处。

An example of setting up tinyMCE with only Bold, Italic and Underline. 仅使用Bold,Italic和Underline设置tinyMCE的示例。

tinyMCE.init({
    // General options
    mode : "textareas",
    theme : "advanced",

    // Theme options
    theme_advanced_buttons1 : "bold,italic,underline"
});

Please try script given below... 请尝试以下给出的脚本......

<script>
 function formatText (key){

    var elem = document.my.textarea; 
    /*start of selection area*/ 
    var start = elem.selectionStart;
    var end = elem.selectionEnd;
    var len = elem.value.length;
    var sel_txt = elem.value.substring(start,end);

    if (sel_txt != ""){
      var begin_tag = "<"+key+">"; 
      var close_tag =  "</"+key+">";
      var replace = begin_tag + sel_txt + close_tag;
      elem.value = elem.value.substring(0,start) + replace + elem.value.substring(end,len);
   }
</script>

I have tried script on http://jsfiddle.net/VRqU3/ . 我在http://jsfiddle.net/VRqU3/上试过脚本。 Please check it. 请检查一下。

I hope it will be work for you, thank you, 我希望它对你有用,谢谢你,

This would be relatively simple were it not for IE < 9, which does not support selectionStart and selectionEnd properties of textarea elements. 如果不是IE <9,这将是相对简单的,它不支持textarea元素的selectionStartselectionEnd属性。 Here's a previous answer of mine detailing how to do this: 以下是我之前的答案,详细说明了如何执行此操作:

https://stackoverflow.com/a/10739034/96100 https://stackoverflow.com/a/10739034/96100

Try this function: 试试这个功能:

function formatText(tag) {
   var Field = document.getElementById('text');
   var val = Field.value;
   var selected_txt = val.substring(Field.selectionStart, Field.selectionEnd);
   var before_txt = val.substring(0, Field.selectionStart);
   var after_txt = val.substring(Field.selectionEnd, val.length);
   Field.value = before_txt + '<' + tag + '>' + selected_txt + '</' + tag + '>' + after_txt;
}

Example: http://jsbin.com/ilecug/ 示例: http//jsbin.com/ilecug/
Source: http://jsbin.com/ilecug/edit 资料来源: http//jsbin.com/ilecug/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM