为什么我可以用这个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
答案 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; }