如何在内容可编辑div中的HTML标记内调用函数?

时间:2014-02-13 23:05:20

标签: javascript jquery contenteditable

为什么我可以用这个html调用myFunction()onkeyup:

<div contenteditable="true" onkeyup="myFunction()" >A function is triggered when the user is pressing a key in the input field.</div>

但我不能用它来做:

<div contenteditable="true">A function is <span onkeyup="myFunction()" >triggered</span> when the user is pressing a key in the input field.</div>

在第二种情况下,我试图仅针对特定单词triggered调用它,我不确定区别是什么。谢谢。

查看FIDDLE

1 个答案:

答案 0 :(得分:0)

试试这个:

HTML(请注意,您必须在跨度上设置contenteditable,而不是父段落,并且不能同时设置两者):

<p>A function is <span class="bold" contenteditable="true" onkeyup="myFunction()" >triggered</span> when the user is pressing a key in the input field.</p>

<p contenteditable="true" onkeyup="myFunction()" >A function is triggered when the user is pressing a key in the input field.</p>

<div id='foo'>Not Yet</div>

CSS:

.bold { font-weight: bold; }

小提琴:http://jsfiddle.net/wTy3C/2/