您好,欢迎访问一九零五行业门户网

Jquery中关于blur和focus事件无法生效的解决办法

jquery blur和focus事件无法生效
$(function(){ alert("页面生效"); $(".login_shopcart").blur(function(){ alert("blur生效"); $(".shopcart_img").attr("src","myimg/shopcart_img.png"); $("#shopcart_num").show(); }); $(".login_shopcart").focus(function(){ alert("focus生效"); $(".shopcart_img").attr("src","myimg/login.png"); $("#shopcart_num").hide(); }); });
代码如上:第一个页面alert()能够生效,但是blur和focus无法生效。我用的是jquery1.9.1版本。
求大神指点
页面html呢? 得触发 .login_shopcart 这个元素才行
这段代码是写在引入的js文件里面
你把html也发出来呀。不然咋知道是你js的问题还是你html的问题
说不定你页面压根就没 class="login_shopcart" 的 对象
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> <script type="text/javascript"> $(function(){ alert("页面生效"); $(".login_shopcart").blur(function(){ alert("blur生效"); $(".shopcart_img").attr("src","myimg/shopcart_img.png"); $("#shopcart_num").show(); }); $(".login_shopcart").focus(function(){ alert("focus生效"); $(".shopcart_img").attr("src","myimg/login.png"); $("#shopcart_num").hide(); }); $(document).ready(function(){ alert("document生效"); $(".login_shopcart").ready(function(){ }); }); }); </script> </head> <body> <div class="login_shopcart" style="position:fixed;bottom:0px;right:100px;"> <p style="position: absolute;padding: 10px 0 0 40px;color:white;" id="shopcart_num">0</p> <img alt="#" src="myimg/shopcart.png" class="shopcart_img"> </div> </body> </html>
而且div元素没有blur和focus事件
jquery导入也没法运行
拜托,,div的话,用
$(function () { alert("页面生效"); $(".login_shopcart").hover(function () { alert("blur生效"); $(".shopcart_img").attr("src", "myimg/shopcart_img.png"); $("#shopcart_num").show(); }, function () { alert("focus生效"); $(".shopcart_img").attr("src", "myimg/login.png"); $("#shopcart_num").hide(); }); });
不知道你是要鼠标悬浮还是鼠标点击
思路:获取焦点 login_shopcart 直接绑定click
失去焦点 document绑定click,让login_shopcart点击无效
部分代码例子:
$(document).click(function(){ $("#login_shopcart").click(function(){ return false; }); $(".shopcart_img").attr("src","myimg/shopcart_img.png"); $("#shopcart_num").show(); });
感谢各位大神~~~找到原因了。div确实无法使用blur和focus~~~改成mouseover 和mouseout生效了~~
以上就是jquery中关于blur和focus事件无法生效的解决办法的详细内容。
其它类似信息

推荐信息