Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 可以通过传递函数(this.value)在HTML for DIV中触发onchange;_Javascript_Html_Jquery_Css_Dom - Fatal编程技术网

Javascript 可以通过传递函数(this.value)在HTML for DIV中触发onchange;

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

下面是一些简单的代码,当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="" 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很棒!那么,请将答案标记为正确,以便其他人知道问题已解决。:)