Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中围绕文本的某个部分包装一个跨距?_Javascript_Jquery_Dom - Fatal编程技术网

如何在javascript中围绕文本的某个部分包装一个跨距?

如何在javascript中围绕文本的某个部分包装一个跨距?,javascript,jquery,dom,Javascript,Jquery,Dom,我有这样的标记: <p>one two three four</p> 一二三四 我想使用javascript将其转换为: <p>one <span>two three<span> four</p> 一二三四 我有我想要在一个跨度中包裹的截面的偏移量和长度,在本例中是offset=4和length=9。如果使用jQuery可以使它更简单,那就更好了。这不是问题的一点重复吗 像这样的东西可能会有帮助 或者,编写自己的jq

我有这样的标记:

<p>one two three four</p>
一二三四

我想使用javascript将其转换为:

<p>one <span>two three<span> four</p>
一二三四


我有我想要在一个跨度中包裹的截面的偏移量和长度,在本例中是
offset=4
length=9
。如果使用jQuery可以使它更简单,那就更好了。

这不是问题的一点重复吗

像这样的东西可能会有帮助

或者,编写自己的jquery函数:
类似(未经测试的代码,可以随意编辑):

jQuery.fn.addSpan = function (elName, str, offset, length)
{
    if(this.innerHTML = str)
    {
        return this.each(function ()
        {
            this.innerHTML = this.innerHTML.substring(0,offset) + "<span>" + this.innerHTML.substring(offset, offset+length) + "</span>" + this.innerHTML.substring(offset+length));
        });
    }
};
$("p").addSpan("one two three four", 4, 9);