簡體   English   中英

可以通過傳遞函數(this.value)在 DIV 的 HTML 內觸發 onchange;

[英]Can onchange be triggered within HTML for DIV by passing function(this.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>

這可以在不使用事件偵聽器的情況下完成嗎?

由於您不想使用事件偵聽器,因此可以使用onInput屬性,如下所示:

 function myFunction(el) { alert("The input value has changed. The new value is: " + el.innerHTML); }
 <,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="" oninput="myFunction(this)" contenteditable="true">Change Value by clicking here</div> </body> </html>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM