JQ中使用on()绑定多个事件

1.多个事件绑定同一函数时,如:但点击或者移入时,被选中元素改变字体色。

点击前

点击或移入后

<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("li").on("click mouseover",function(){
   $(this).addClass('selected');
   $(this).siblings().removeClass('selected')
  });
});

</script>
<style type="text/css">
.selected{color:red;}
li{color:green;width:100px;height:40px}
</style>
</head>
<body>
    <div>
        <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
    </div>
</body>
</html>

2.多个事件绑定不同函数时,如:但点击时被选中元素字体变大(点击试也移入了故字体也变红,这里只是随意选择了两个事件),移入时字体色变为红色。

点击移入前

点击后

 

 

移入后(重新运行,故点击后的更改消失) 

 

<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("li").on({"click":function(){
   $(this).css("font-size","larger")
  },"mouseover":function(){
   $(this).css("color","red")
  }});
});

</script>
<style type="text/css">
li{color:green;width:100px;height:40px}
</style>
</head>
<body>
<div>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
    </ul>
</div>
</body>
</html>

$(selector).on(event,childSelector,data,function),其中event为必需。规定要从被选元素添加的一个或多个事件或命名空间。由空格分隔多个事件值,也可以是数组。必须是有效的事件

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值