3d科技网
您的当前位置:首页Jquery中关于blur和focus事件无法生效的解决办法

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

来源:3d科技网


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生效了~~

显示全文