这个 改变 是jQuery中的一个内置方法,用于检测输入字段值的变化。此方法仅适用于“ ,
null
语法:
$(selector).change(function)
参数: 它接受一个可选参数“function”。 返回值: 它返回修改后的元素。 显示change()方法工作的jQuery代码: 代码#1: 在下面的代码中,没有函数传递给change方法。
< html > < head > jquery/3.3.1/jquery.min.js"></ script > <!--Demo of change method without passing function--> < script > $(document).ready(function() { $("button").click(function() { $("input").change(); }); }); </ script > </ head > < body > < p >Click the button to see the changed value !!!</ p > <!--click on this button and see the change --> < button >Click Me!</ button > < p >Enter value: < input value = "Donald" onchange = "alert(this.value)" type = "text" ></ p > </ body > </ html > |
输出: 单击“单击我”按钮之前-
点击“点击我”按钮后-
代码#2: 在下面的代码中,函数被传递给change方法。
< html > < head > jquery/3.3.1/jquery.min.js"></ script > <!--Here function is passed to the change method--> < script > $(document).ready(function() { $(".field").change(function() { $(this).css("background-color", "#7FFF00"); }); }); </ script > < style > .field { padding: 5px; } </ style > </ head > < body > <!--write something and click outside --> Enter Value: < input class = "field" type = "text" > < p >Write something in the input field, and then press enter or click outside the field.</ p > </ body > </ html > |
输出: 在“输入值:”框中输入任何值之前-
在“输入值:”框中输入“Geeksforgeks”的值,然后在框外单击-
jQuery是一个开源JavaScript库,可以简化HTML/CSS文档之间的交互,它以其独特的编程理念而广为人知 “少写多做” . 通过以下步骤,您可以从头开始学习jQuery jQuery教程 和 jQuery示例 .
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END