简体   繁体   English

如何使用JavaScript或jQuery将选定的文本包装到特定标签的网页中?

[英]How to use JavaScript or jQuery to wrap the selected text into a web page in a specific tag?

So I need to find a way to wrap the selected (highlighted) text in a web page into a specific tag using javascript or jQuery. 因此,我需要找到一种使用javascript或jQuery将网页中选定的(突出显示的)文本包装到特定标签中的方法。 I managed to wrap some text into the tag, but the problem is that whenever my selection includes two different paragraphs, the selected text becomes new paragraphs (ie everything becomes a mess). 我设法将一些文本包装到标签中,但是问题是,只要我的选择包括两个不同的段落,所选的文本就会变成新的段落(即,所有内容都变成一团糟)。 This is what I got (I am using the code inside a Google Chrome extension): 这就是我得到的(我正在使用Google Chrome扩展程序中的代码):

var sel = window.getSelection();
var rng = sel.getRangeAt(0);
var wrappingNode = document.createElement("myElement");
wrappingNode.appendChild(rng.extractContents());
rng.insertNode(wrappingNode);

I'm trying to find a way to prevent messing up the web page structure. 我正在尝试找到一种防止弄乱网页结构的方法。 Do you guys have any idea?? 你们有什么主意吗?

My objective is to apply a specific css style to this selected text. 我的目标是对选定的文本应用特定的CSS样式。

Thank You! 谢谢!

FF only example because of range.createContextualFragment but something to work with. FF仅作为示例,因为range.createContextualFragment但可以使用。 got to get to sleep! 要睡觉了!

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        .highlight {color: red; }
    </style>
    <script>
        function highlight() {
            var text, sel, range;
            if (window.getSelection) {
                text = window.getSelection().toString();
                sel = window.getSelection();
                if (sel.rangeCount) {
                    range = sel.getRangeAt(0);
                    range.deleteContents();
                    range.insertNode(range.createContextualFragment('<span class="highlight">'+text+'</span>'));
                }
            } else if (document.selection && document.selection.createRange) {
                text = document.selection.createRange().text;
                range = document.selection.createRange();
                range.innerHTML = '<span class="highlight">'+text+'</span>';
            }
        }
    </script>
</head>

<body>
<p>lorem ipsum donor kebab</p>
<button onclick="highlight()">Click me</button>
</body>
</html>

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

相关问题 Javascript / jquery-在标签的特定位置换行 - Javascript/jquery - Wrap text with tag at specific position jQuery:如何包装所选文本 - jQuery: how to wrap selected text 如何使用javascript / jquery包装带有标签的元素的同级文本? - How to wrap sibling text of elements with tag using javascript/jquery? 如何更改网页上某个特定字母的颜色(包装文本)? - How to change the color of one specific letter on a web page (wrap the text)? Javascript:如何将带有多个p标签的选定文本包装到带有span标签的每个标签的每个内容中 - Javascript: How to wrap selected text along multiple p tags into each content for each tag with span tag 使用javascript和jquery获取网页中选定文本的上下文 - Get the context of selected text in web page using javascript and jquery 使用javascript仅包含带有html标记的选定文本 - Wrap only selected text with a html tag using javascript javascript 用标签换行文本 - javascript wrap text with tag 如何使用jquery或javascript将锚标记添加到段落中的选定文本 - How to add a anchor tag to selected text in the paragraph using jquery or javascript 如何使用 PHP 或 JavaScript 在网页正文中显示描述元标记的内容? - How can I use either PHP or JavaScript to display the contents of the description meta tag in the body text of a web page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM