Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/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
在一个单词的中间放置一个div 我尝试使用JavaScript在div的中间放置div,同时点击那个div,但是我有问题。_Javascript_Html - Fatal编程技术网

在一个单词的中间放置一个div 我尝试使用JavaScript在div的中间放置div,同时点击那个div,但是我有问题。

在一个单词的中间放置一个div 我尝试使用JavaScript在div的中间放置div,同时点击那个div,但是我有问题。,javascript,html,Javascript,Html,示例代码: <div>This is example text</div> 这是示例文本 我点击m上的单词example,我希望它看起来像这样 <div>This is exam<span id='cursor'></span>ple text</div> 这是示例文本 现在,当我再次单击div时,我希望跨度移动到新位置: <div>This i<span id='cursor'></s

示例代码:

<div>This is example text</div>
这是示例文本
我点击m上的单词example,我希望它看起来像这样

<div>This is exam<span id='cursor'></span>ple text</div>
这是示例文本
现在,当我再次单击div时,我希望跨度移动到新位置:

<div>This i<span id='cursor'></span>s example text</div>
这是示例文本

我怎样才能做到这一点呢?

刚刚在code pen上做了一些测试,我希望这就是您想要的

<div id="lol">This is exa<span id="cut" onclick="split()">m</span>ple text</div>
<script>
function split()
{
document.getElementById("lol").innerHTML = "This is exam ple text";
}
</script>

When the "m" which is spanned is clicked it doesn't move the span, but it replaces the text  which still plays the same role you explained that you want. If you want it to repeat this, just put the span with the onclick action in a different place, or have multiple
这是示例文本
函数拆分()
{
document.getElementById(“lol”).innerHTML=“这是示例文本”;
}
单击跨距的“m”时,它不会移动跨距,但它会替换仍然扮演与您所解释的相同角色的文本。如果希望它重复此操作,只需将带有onclick操作的span放在不同的位置,或者有多个

您是否可以使用目前为止的代码?你在使用选择API吗?我刚刚更新了我的帖子来帮助回答这个问题看看