【问题标题】:Can onchange be triggered within HTML for DIV by passing function(this.value);可以通过传递 function(this.value) 在 HTML 中为 DIV 触发 onchange;
【发布时间】:2021-08-09 09:19:00
【问题描述】:

这里有一些简单的代码,当 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>

不使用事件监听器可以做到这一点吗?

【问题讨论】:

标签: javascript html jquery css dom


【解决方案1】:

由于您不想使用事件侦听器,因此可以使用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>

【讨论】:

  • @DracoMalfoy 太棒了!然后,请将答案标记为正确,以便其他人知道问题已解决。 :)
猜你喜欢
  • 1970-01-01
  • 2017-08-19
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-13
  • 2012-10-17
  • 1970-01-01
相关资源
最近更新 更多