为什么我可以使用以下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

最佳答案

尝试这个:

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/

10-08 14:38
查看更多