jQuery hover 函数 -- 通过绑定变量"handlerInOut"来切换方法 本帖最后由 lslyhj 于 2014-07-27 17:46:29 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <html><head><style type="text/css">.s2{background-color:yellow!important;}.s4{background-color:aqua;}</style><script type="text/javascript" src="jquery-1.7.2.js"></script><head/><body><div class="s4" >hello jQuery 4</div><br /><script type="text/javascript">// var handlerInOut = function(){alert("111");};function handlerInOut(){ $(this).toggleClass('s2');}$('.s4').hover(handlerInOut);</script></body></html> 貌似你绑定事件的时候页面还没加载完呢 第一个问题是因为你绑定了mouseover mouseleave hover 第一、你事件绑定像楼上说的,事件绑定时页面还没有加载完第二、针对你第一个问题,你先绑定了$('.s4').bind('mouseover mouseleave',handlerInOut); 又绑定$('.s4').hover(handlerInOut);这两个效果一样,你相当于同一个事件执行了两次第三、针对第二个问题,没啥可说了,按上面改完就ok了附代码<html><head><style type="text/css">.s2{background-color:yellow!important;}.s4{background-color:aqua;}</style><script type="text/javascript" src="jquery-1.7.2.min.js"></script><script type="text/javascript">// var handlerInOut = function(){alert("111");};$(document).ready(function(){ function handlerInOut(){ //alert("222"); $(this).toggleClass('s2'); } $('.s4').bind('mouseover mouseleave',handlerInOut); //$('.s4').hover(handlerInOut);});</script><head/><body><div class="s4" >hello jQuery 4</div><br /></body></html> 正则表达式的疑问 [效果]点击图片缩小放大 图片跟随鼠标移动 这个表单验证怎么回事 上下焦点不停切换死循环 undefined为空或不是对象 如何将-moz-opacity放入jquery中的 json数据 谁能在 火狐浏览器下兼容下 FF 代码很简单 js怎么取得tr td 标记里面的 值 ?? 暴强的免费AJAX,Javascript IDE,大家都来下载 移动图片会出现抖动现象 JS文件的组成特点是什么? 动态创建树节点 ExtJS4 大神进来帮帮我呀 一个简单的时钟 就是不能运行 呜呜
<head>
<style type="text/css">
.s2{background-color:yellow!important;}
.s4{background-color:aqua;}
</style>
<script type="text/javascript" src="jquery-1.7.2.js"></script><head/>
<body>
<div class="s4" >hello jQuery 4</div><br />
<script type="text/javascript">
// var handlerInOut = function(){alert("111");};
function handlerInOut(){
$(this).toggleClass('s2');
}
$('.s4').hover(handlerInOut);
</script>
</body>
</html>
貌似你绑定事件的时候页面还没加载完呢 第一个问题是因为你绑定了mouseover mouseleave hover
第二、针对你第一个问题,你先绑定了$('.s4').bind('mouseover mouseleave',handlerInOut);
又绑定$('.s4').hover(handlerInOut);这两个效果一样,你相当于同一个事件执行了两次
第三、针对第二个问题,没啥可说了,按上面改完就ok了
附代码<html>
<head>
<style type="text/css">
.s2{background-color:yellow!important;}
.s4{background-color:aqua;}
</style>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
// var handlerInOut = function(){alert("111");};$(document).ready(function(){
function handlerInOut(){
//alert("222");
$(this).toggleClass('s2');
}
$('.s4').bind('mouseover mouseleave',handlerInOut);
//$('.s4').hover(handlerInOut);
});</script>
<head/>
<body>
<div class="s4" >hello jQuery 4</div><br />
</body>
</html>