用onclick在一个单词的中间放置一个div

时间:2014-05-14 23:43:04

标签: javascript html

我正在尝试使用JavaScript在点击该div时将div放在另一个div的中间,但是我遇到了问题。

示例代码:

<div>This is example text</div>

然后我点击m上的单词示例,我希望它看起来像这样,

<div>This is exam<span id='cursor'></span>ple text</div>

现在当我再次点击div时,我希望跨度移动到新位置:

<div>This i<span id='cursor'></span>s example text</div>

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

刚刚对代码笔进行了一些测试,我希望这是你正在寻找的东西

<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