【问题标题】:jQuery: how to wrap selected textjQuery:如何包装选定的文本
【发布时间】:2013-11-14 08:53:09
【问题描述】:

我想使用下面的函数来获取当前选中的文本(来源:javascript replace selection all browsers)。

在我的情况下,我不想替换选定的文本,而是想在其前后添加一些内容,主要是 HTML 标记(通过单击按钮)。

示例: 选定的文本 = 你好 新文本 (html) = <u>Hello</u>

获取选定文本的代码:

  function replaceSelection(html) {
        var sel, range, node;

        if (typeof window.getSelection != "undefined") {
            // IE 9 and other non-IE browsers
            sel = window.getSelection();

            // Test that the Selection object contains at least one Range
            if (sel.getRangeAt && sel.rangeCount) {
                // Get the first Range (only Firefox supports more than one)
                range = window.getSelection().getRangeAt(0);
                range.deleteContents();

                // Create a DocumentFragment to insert and populate it with HTML
                // Need to test for the existence of range.createContextualFragment
                // because it's non-standard and IE 9 does not support it
                if (range.createContextualFragment) {
                    node = range.createContextualFragment(html);
                } else {
                    // In IE 9 we need to use innerHTML of a temporary element
                    var div = document.createElement("div"), child;
                    div.innerHTML = html;
                    node = document.createDocumentFragment();
                    while ( (child = div.firstChild) ) {
                        node.appendChild(child);
                    }
                }
                range.insertNode(node);
            }
        } else if (document.selection && document.selection.type != "Control") {
            // IE 8 and below
            range = document.selection.createRange();
            range.pasteHTML(html);
        }
    }

调用函数的代码:

replaceSelection('<span><font color="red">hoho</font></span>');

有人可以告诉我如何通过修改上述内容来实现这一目标吗?

非常感谢您提供的任何帮助,蒂姆。

【问题讨论】:

  • 您用 jquery 标记了您的答案,但我没有看到您正在使用它。使用 jQuery,这个问题会很容易解决。
  • 谢谢。我一般都在使用 jQuery,只是这里还没有包含它。如果有办法以这种方式解决它,我很乐意使用它。

标签: javascript jquery html replace getselection


【解决方案1】:

jQuery 的 append() 和 prepend() 应该可以解决问题。

$(".my-span").prepend("<u>").append("</u>");

HTML:

<div>
  <span class="my-span">Hello</span>
</div>

【讨论】:

  • 谢谢。我如何将其链接到上述内容?
  • .my-span 部分更改为您想要添加和添加的任何元素。将. 放在class 名称之前或将# 放在id 之前。
  • $(".my-span").wrap("u")
【解决方案2】:

我认为这可能对你有帮助

HTML

<div id="abc">I am a div.</div>
<br />
<input value="Add Before" id="before" type="submit">
<input value="Add After" id="after" type="submit">
<input value="InSide Before" id="inSideB" type="submit">
<input value="To Replace With Html" id="inSideRpHtml" type="submit">
<input value="To Replace With Text" id="inSideRpText" type="submit">

JQ

$(document).ready(function(){

   //for adding Before
   $("#before").click(function () {
$("#abc").before("Before<br />");
});
   //for adding After
   $("#after").click(function () {
$("#abc").after("After<br />");
});
    //for adding Inside before
    $("#inSideB").click(function () {
$("#abc").prepend("New Content!");
});

     //for adding Inside after 
    $("#inSideA").click(function () {
$("#abc").append("New Content!");
});
    // for Replacing with Html content
    $("#inSideRpHtml").click(function () {
$("#abc").html("<span><font color=red>hoho</font></span>");
});
   // for Replacing with Text content
    $("#inSideRpText").click(function () {
$("#abc").text("<span><font color=red>this is text");
});

});

查看此链接 Here is the Live Demo

如果这是您需要的,请回复..

【讨论】:

  • 谢谢。我的想法是同时做这两件事,即同时添加和添加。我会检查是否可以使用您的代码以这种方式工作。
  • 我在 JSFiddle 上更新了更多代码。您可以使用相同的链接或CheckThis.☺ 进行检查
  • 谢谢,Manoj。这里的问题是它使用了整个 div,但我需要它只处理选定的文本。上面的函数会告诉我我选择了什么,所以问题只是如何将它与你的函数结合起来,以便它在所选文本(而不是 div)之前添加一些内容。
  • 嗨,我想我为你找到了解决方案Check This link。如果匹配,请回复我..
猜你喜欢
  • 1970-01-01
  • 2010-12-15
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 1970-01-01
  • 2019-08-30
  • 2012-03-14
  • 2019-04-09
相关资源
最近更新 更多