本文实例分析了jquery中change()的用法。分享给大家供大家参考。具体分析如下:
change()当元素的值发生改变时,会发生 change 事件。该事件仅适用于文本域(text field),以及 textarea 和 select 元素。
当用于 select 元素时,change 事件会在选择某个选项时发生。当用于 text field 或 text area 时,该事件会在元素失去焦点时发生。
一、change的用法
1、触发 change 事件:触发被选元素的 change 事件
语法: $(selector).change()
2、将函数绑定到 change 事件:规定当被选元素的 change 事件发生时运行的函数。
语法: $(selector).change(function)
二、jquery中change()实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <html>
<head>
<script type= "text/javascript" src= "jquery文件" ></script>
<script type= "text/javascript" >
$(document).ready( function (){
$( ".field" ).change( function (){
$(this).css( "background-color" , "#FFFFCC" );
});
$( "button" ).click( function (){
$( "input" ).change();
});
});
</script>
</head>
<body>
<button>激活文本域的 change 事件</button>
<p>Enter your name: <input class = "field" type= "text" /></p>
</body>
</html>
|
登录后复制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <html>
<head>
<script type= "text/javascript" src= "jquery文件" ></script>
<script type= "text/javascript" >
$(document).ready( function (){
$( ".field" ).change( function (){
$(this).css( "background-color" , "#FFFFCC" );
});
});
</script>
</head>
<body>
<p>在某个域被使用或改变时,它会改变颜色。</p>
Enter your name: <input class = "field" type= "text" />
<p>Car:
<select class = "field" name= "cars" >
<option value= "volvo" >Volvo</option>
<option value= "saab" >Saab</option>
<option value= "fiat" >Fiat</option>
<option value= "audi" >Audi</option>
</select>
</p>
</body>
</html>
|
登录后复制
希望本文所述对大家的jQuery程序设计有所帮助。