视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
Jquery中关于blur和focus事件无法生效的解决办法
2020-11-27 20:18:34 责编:小采
文档


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

下载本文
显示全文
专题