Web Developer显示我的JavaScript是有效的,但如果我运行该页面,这将不起作用。我尝试在jquery-color的网站上跟踪使用,但每次都返回属性id missing。我真的希望我在大学里学习JavaScript的时候能有一个更好的老师。他闪现了一下jQuery和JavaScript的大部分内容,但并没有真正地教它。
编辑#1:我修复了代码中的(this)错误,但仍然没有成功。
下面是jQuery的代码:
<script type="text/javascript">
jQuery("li.site-links").hover(function(){
jQuery(this).stop().animate({
backgroundColor: "#000000"
}, 1000 );
});
</script>和站点链接:http://lab.nmjgraphics.com
发布于 2013-01-30 15:43:53
试试这个:
jQuery(function(){
jQuery("a.site-links").hover(function(){
jQuery(this).closest('li').stop().animate({
backgroundColor: "#000000"
}, 1000 );
},function(){
jQuery(this).closest('li').stop().animate({
backgroundColor: "transparent"
}, 1000 );
});
});您可以尝试使用.parent('li')
或者试试这个:
jQuery(function(){
jQuery("a.site-links").parent('li').hover(function(){
jQuery(this).stop().animate({
backgroundColor: "#000000"
}, 1000 );
},function(){
jQuery(this).closest('li').stop().animate({
backgroundColor: "transparent"
}, 1000 );
});
});发布于 2013-01-30 15:42:47
您需要在选择器中将"this"更改为this才能访问事件源。在选择器中使用" this“将搜索标签名称,就像jQuery("input")将获得名称为input的所有标签一样。
变化
jQuery("this")至
jQuery(this)您可以通过here查看"this"与this之间的差异
发布于 2013-01-30 15:58:32
首先:你的选择器是错误的。您没有带有.site-link的<a>。你有一个带有.site链接的<li><a>
因此:
$(".site-links").hover(function(){
jQuery(this).parent().stop().animate({
backgroundColor: "#000"
}, 1000 );
});这是一个不透明的演示:http://jsfiddle.net/2VgBa/
https://stackoverflow.com/questions/14598893
复制相似问题