Javascript 可以通过传递函数(this.value)在HTML for DIV中触发onchange;
下面是一些简单的代码,当div标记文本发生更改时,应发出警报:Javascript 可以通过传递函数(this.value)在HTML for DIV中触发onchange;,javascript,html,jquery,css,dom,Javascript,Html,Jquery,Css,Dom,下面是一些简单的代码,当div标记文本发生更改时,应发出警报: <!DOCTYPE html> <html> <body> <p>Modify the text by clicking on it, then click outside the field to fire the onchange event.</p> <div type="text" name="txt" value
<!DOCTYPE html>
<html>
<body>
<p>Modify the text by clicking on it, then click outside the field to fire the onchange event.</p>
<div type="text" name="txt" value="" onchange="myFunction(this.value)" contenteditable="true">Change Value by clicking here</div>
<script>
function myFunction(val) {
alert("The input value has changed. The new value is: " + val);
}
</script>
</body>
</html>
通过单击文本来修改文本,然后在字段外单击以触发onchange事件
单击此处更改值
函数myFunction(val){
警报(“输入值已更改。新值为:“+val”);
}
不使用事件监听器可以做到这一点吗?因为您不想使用事件监听器,所以可以使用
onInput
属性,如下所示:
函数myFunction(el){
警报(“输入值已更改。新值为:“+el.innerHTML”);
}
通过单击文本来修改文本,然后在字段外单击以触发onchange事件
单击此处更改值
尝试一下@DracoMalfoy很棒!那么,请将答案标记为正确,以便其他人知道问题已解决。:)