转载 

jQuery 1.9/2.0/2.1及其以上 on 无效的解决办法

分类:    2351人阅读    IT小君  2016-11-28 14:53

写成这个形式就OK了:

$(document).on("click",".test",function(){//修改成这样的写法
    $(".test").css("background-color","pink");
  });

jQuery 1.9/2.0/2.1及其以上版本无法使用live函数了,然而jQuery 1.9及其以上版本提供了on函数来代替。本文讲解了jQuery on函数的使用方法,以及在使用jQuery函数中遇到的一些问题。

$(selector).on(event,childSelector,data,function,map)
各个参数说明如下:


参数 描述
event 必需。规定要从被选元素移除的一个或多个事件或命名空间。由空格分隔多个事件值。必须是有效的事件。
childSelector 可选。规定只能添加到指定的子元素上的事件处理程序(且不是选择器本身,比如已废弃的 delegate() 方法)。
data 可选。规定传递到函数的额外数据。
function 可选。规定当事件发生时运行的函数。
map 规定事件映射 ({event:function, event:function, …}),包含要添加到元素的一个或多个事件,以及当事件发生时运行的函数。
按照上面的语法下面的例子是可以实现的


<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("p").on("click",function(){
    alert("The paragraph was clicked.");
  });
});
</script>
</head>
<body>
 
<p>Click this paragraph.</p>
 
</body>
</html>
但是如果要绑定的on方法是动态加载出来的元素,那么这样使用就是没有用的。看下面的例子:


<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#div1").click(function(){
    $("<div class='test'>test</div>").appendTo($("#div1"));
  });
  $(".test").on("click",function(){
    $(".test").css("background-color","pink");
  });
  $("#div2").bind("click",function(){
    $(this).css("background-color","pink");
  });
});
</script>
</head>
<body>
 
<h4 style="color:green;">This example demonstrates how to achieve the same effect using on() and bind().</h4>
 
<div id="div1" style="border:1px solid black;">This is some text.
<p>Click to set background color using the <b>on() method</b>.</p>
</div><br>
 
<div id="div2" style="border:1px solid black;">This is some text.
<p>Click to set background color using the <b>bind() method</b>.</p>
</div>
 
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#div1").click(function(){
    $("<div class='test'>test</div>").appendTo($("#div1"));
  });
  $(".test").on("click",function(){
    $(".test").css("background-color","pink");
  });
  $("#div2").bind("click",function(){
    $(this).css("background-color","pink");
  });
});
</script>
</head>
<body>
 
<h4 style="color:green;">This example demonstrates how to achieve the same effect using on() and bind().</h4>
 
<div id="div1" style="border:1px solid black;">This is some text.
<p>Click to set background color using the <b>on() method</b>.</p>
</div><br>
 
<div id="div2" style="border:1px solid black;">This is some text.
<p>Click to set background color using the <b>bind() method</b>.</p>
</div>
 
</body>
</html>

上面例子中.test元素是动态加载的,但是给它绑定click方法的时候,明明使用了


$(".test").css("background-color","pink");
将背景色设为pink,但是没有起作用,什么原因呢,原因就在于.test是动态加载的元素,而使用上面的方法不能绑定动态加载元素的事件,修正的方法为使用下面的代码代替:



<!DOCTYPE html>
<html>
<head>
<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#div1").click(function(){
    $("<div class='test'>test</div>").appendTo($("#div1"));
  });
  $(document).on("click",".test",function(){//修改成这样的写法
    $(".test").css("background-color","pink");
  });
  $("#div2").bind("click",function(){
    $(this).css("background-color","pink");
  });
});
</script>
</head>
<body>
 
<h4 style="color:green;">This example demonstrates how to achieve the same effect using on() and bind().</h4>
 
<div id="div1" style="border:1px solid black;">This is some text.
<p>Click to set background color using the <b>on() method</b>.</p>
</div><br>
 
<div id="div2" style="border:1px solid black;">This is some text.
<p>Click to set background color using the <b>bind() method</b>.</p>
</div>
 
</body>
</html>
究其元素就在于使用$(document)意义就在于使元素加载完后才执行方法,所以当为jQuery动态加载的元素绑定on方法的时候,使用$(document)设置代码脚本在DOM元素加载完成后开始执行。



点击广告,支持我们为你提供更好的服务

html5 canvas彩色碎片组合球形旋转动画特效

网页设计开发公司网站模板

中小型创意设计服务公司网站模板

css鼠标跟随文字模糊特效

canvas炫酷鼠标移动文字粒子特效

响应式咖啡饮品宣传网站模板

HTML5 Canvas竖直流动线条背景动画特效

现代时尚家具公司网站模板

jQuery右端悬浮带返回顶部特效

小众时尚单品在线电子商务网站模板

HTML5现代家居装潢公司网站模板

html5 svg夜空中星星流星动画场景特效

有机水果蔬菜HTML5网站模板

css+js实现的颜色渐变数字时钟动画特效

响应式时尚单品在线商城网站模板

响应式太阳能能源公司网站模板

html5图标下拉搜索框自动匹配代码

js+css3抽奖转盘旋转点餐代码

html5 canvas进度条圆环图表统计动画特效

HTML5数字产品服务公司网站模板

点击广告,支持我们为你提供更好的服务
 工具推荐 更多»
点击广告,支持我们为你提供更好的服务