jQuery .css 颜色更改不起作用
我正在尝试更改 lavalamp 菜单上文本的颜色我正在使用以下插件 http://www.queness.com/post/530/simple-lava-lamp-menu-tutorial-with-jquery
我所做的是以下
$('#lava').mouseleave(function () {
$('#lava li').removeClass('selected');
$('#lava li').css({color: '#FFF'});
//select the current item
$(this).addClass('selected');
$(this).css("color", "white");
});
但是当老鼠离开了它将所有文本更改为黑色,这是正确的,但 $(this) 不会更改为白色
这里是代码和工作演示的副本
I am trying to change the color of my text on my lavalamp menu I am using the following plugin http://www.queness.com/post/530/simple-lava-lamp-menu-tutorial-with-jquery
What I have done is the following
$('#lava').mouseleave(function () {
$('#lava li').removeClass('selected');
$('#lava li').css({color: '#FFF'});
//select the current item
$(this).addClass('selected');
$(this).css("color", "white");
});
however when the mouse leaves it changes all the text to black which is correct but then the $(this) does not change to white
here is a copy of the code and working demo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想你想要的是这样的:
http://jsfiddle.net/aSr3J/20/
本质上,您的 mouseleave 函数必须如下所示
请注意,我还为样式表中的链接添加了颜色定义:(
您是否知道在内联元素中包含像
li
这样的块级元素喜欢a
只在HTML5中有效?)至于菜单文本的颜色我也修改了
$('#lava li').hover(function ())
:I suppose what you're after is this:
http://jsfiddle.net/aSr3J/20/
Essentially your mouseleave function would have to look like this
Note that I have also added a color definition for the links in the style sheet:
(Are you aware that enclosing block-level elements like
li
in inline-elements likea
is only valid in HTML5?)As for the color of the menu text I have also amended the
$('#lava li').hover(function ())
:该代码几乎肯定不正确。他们的关键字“this”是一种神奇的野兽,它可以以令习惯其他语言的程序员非常惊讶的方式发生变化。
首先阅读本文以了解“this”是什么以及它是如何修改的。
http://howtonode.org/what-is-this
然后使用jquery函数代理( http://api.jquery.com/jQuery.proxy/) 将“this”封装到函数中。
The code is almost certainly not correct. They keyword 'this' is a magical beast which can change in ways that are very suprising to programmers used to other languages.
First read this to understand what 'this' is and how it gets modified.
http://howtonode.org/what-is-this
And then use the jquery function proxy (http://api.jquery.com/jQuery.proxy/) to encapsulate 'this' through to the function.
尝试让它在每个li悬停时改变颜色
Try to make it change color on the hover of each li