2014-02-13 40 views
1

爲什麼我可以調用myFunction的()的onkeyup與這個網站:如何在內容可編輯div內的HTML標記中調用函數?

<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

@GSiry'b'實際上是一個HTML元素...並且不會發布w3schools鏈接。 – kapa

+0

@GSiry ^^這個和同一網站實際上與你所說的相矛盾,並說你*可以*用'b'元素來使用鍵盤事件...... http://www.w3schools.com/tags/tag_b.asp – Archer

+0

看http://jsfiddle.net/wTy3C/1/我用跨度和DIV沒有B,它仍然沒有工作 – Squirrl

回答

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/