简体   繁体   English

使用javascript仅包含带有html标记的选定文本

[英]Wrap only selected text with a html tag using javascript

I am trying to wrap only selected text with a specific tag using javascript tag. 我试图使用javascript标签仅包装具有特定标签的选定文本。 I find a code but when I click a button then it will add a tag which I don't want because I only want to apply the tag around selected element. 我找到了一个代码但是当我点击一个按钮然后它会添加一个我不想要的标签,因为我只想在所选元素周围应用标签。

Example JS code: JS代码示例:

function wrapText(elementID, openTag, closeTag) {
    var textArea = $('#' + elementID);
    var len = textArea.val().length;
    var start = textArea[0].selectionStart;
    var end = textArea[0].selectionEnd;
    var selectedText = textArea.val().substring(start, end);
    var replacement = openTag + selectedText + closeTag;
    textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len));
}

$('#bold').click(function() {
    wrapText("myTa", "<strong>", "</strong>");
});
$('#italic').click(function() {
    wrapText("myTa", "<em>", "</em>");
});
$('#underline').click(function() {
    wrapText("myTa", "<u>", "</u>");
});
$('#code').click(function() {
    wrapText("myTa", "<pre><code>", "</code></pre>");
});

This is the Full working code Link 这是完整的工作代码链接

Any help will be appreciated. 任何帮助将不胜感激。

Use this condition: if(start != end){ ... } 使用此条件: if(start != end){ ... }

http://jsfiddle.net/sherali/bd4np/119/ http://jsfiddle.net/sherali/bd4np/119/

function wrapText(elementID, openTag, closeTag) {
    var textArea = $('#' + elementID);
    var len = textArea.val().length;
    var start = textArea[0].selectionStart;
    var end = textArea[0].selectionEnd;
    if (start != end){
       var selectedText = textArea.val().substring(start, end);
       var replacement = openTag + selectedText + closeTag;
       textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len));
    }
}

暂无
暂无

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

相关问题 如何仅将“选定”文本更改为“使用 html 和 JavaScript”输入标签中存在的“粗体/非粗体” - How to change the “selected” text to “bold / unbold” which is present in input tag “using html and JavaScript” only javascript 用标签换行文本 - javascript wrap text with tag 使用 JavaScript 从所选文本中删除 Html 标签 - Remove Html tag from selected text with JavaScript javascript使用正则表达式捕获来包装特定文本,但排除html标签属性 - javascript to wrap specific text using regex capture but exclude html tag attributes 如何使用JavaScript将标签添加到选定的文本 - How to add tag to selected text, using javascript Javascript:如何将带有多个p标签的选定文本包装到带有span标签的每个标签的每个内容中 - Javascript: How to wrap selected text along multiple p tags into each content for each tag with span tag <span>使用 jQuery</span>将特定文本字符串包装在 HTML 标签内 - Wrap a particular text string inside an HTML tag with a <span> using jQuery 使用jquery在文本字符串之间包装一些html标记 - wrap some html tag between string of text by using jquery 如何使用javascript将一组HTML标签包装到包含标签中? - How to wrap a group of HTML tags into a containing tag using javascript? 如何使用javascript / jquery包装带有标签的元素的同级文本? - How to wrap sibling text of elements with tag using javascript/jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM