change 事件适用于文本域在值变化时触发,我们也可以在非标单元素中通过主动调用 change()
方法来触发 change 事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>change事件</title> <style> #container { min-height: 120px; border: 1px solid #aaa; } </style> </head> <body> <div id="container">
</div> <button type="button" id="btn">add "aaa" for div</button> <button type="button" id="attr">监听属性</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script>
function changes(){ alert("changes");
}
$("#btn").click(function() { $("#container").append("aaa").change(); }); $("#attr").click(function() { $("#container").attr("data-value", "test").change(); }); $("#container").bind("change", changes); </script> </body> </html>
|
试一试