JQuery 添加类功能不起作用
我正在尝试创建一个类似于 Twitter 推文区域的元素。鼠标悬停时 DIV 应该是不同的颜色,并且背景图像有新的位置。
然后,当用户单击背景图像位置时,应再次移动,并且仅当用户未悬停时背景颜色才应更改。我尝试过这个但行不通。
JS --
function reviews(id) {
$('#reviews .review').removeClass('ractive');
$(id).addClass('ractive');
}
CSS --
#reviews .review {
font-family:"Lucida Grande", Arial, sans-serif;
display:block;
width:599px;
padding-right:30px;
background:url(images/ui/arrows.png) -60px -60px no-repeat;
}
#reviews .review:hover {
background-position:605px 50px;
background-color:#E6F5F5;
}
#reviews .review .ractive {
background-color:#E2E2E2;
background-position:605px -100px;
}
#reviews .review .ractive:hover {
background-color:#E6F5F5;
background-position:605px -100px;
}
HTML
<div class="review" onclick="reviews(this);">Blah Blah Blah Blah</div>
任何想法出了什么问题。新类不会应用于该元素。
奇妙
I am trying to have an element rather like the Twitter tweets area. The DIV should be a different colour on mouseover with a new position for the background image.
Then when the user clicks the background image position should move again and the background colour should change only when the user is not on hover. I tried this but it will not work.
JS --
function reviews(id) {
$('#reviews .review').removeClass('ractive');
$(id).addClass('ractive');
}
CSS --
#reviews .review {
font-family:"Lucida Grande", Arial, sans-serif;
display:block;
width:599px;
padding-right:30px;
background:url(images/ui/arrows.png) -60px -60px no-repeat;
}
#reviews .review:hover {
background-position:605px 50px;
background-color:#E6F5F5;
}
#reviews .review .ractive {
background-color:#E2E2E2;
background-position:605px -100px;
}
#reviews .review .ractive:hover {
background-color:#E6F5F5;
background-position:605px -100px;
}
HTML
<div class="review" onclick="reviews(this);">Blah Blah Blah Blah</div>
Any ideas what is wrong. The new class does not seam to be applied to the element.
Marvellous
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有得到合适的选择器。从你的标记来看你应该这样做。
然后,您应该从您的标记中删除
onclick="reviews(this);
。如果您使用 jquery 来应用类,请总体上坚持该方法。不要混合使用引人注目的和 unobtrusive javascript。上面发布的代码将仅将ractive
类添加到您拥有的链接 。另外,将您的 css 从 #reviews .review .ractive 更改为 #reviews .ractive 。您所拥有的类正在寻找带有类 ractive 的 review 中的元素,您不需要它,因为该类位于同一类上 元素
is not getting a proper selector. From your markup you should be doing.
You should then REMOVE
onclick="reviews(this);
from your markup. If you are using jquery to apply classes, stick with that approach overall. Don't mix obtrusive and unobtrusive javascript. The code posted above will add theractive
class to only the link you have clicked.Also, change your css from #reviews .review .ractive to just #reviews .ractive. The class you have is looking for an element inside review with the class ractive, you don't need that as the class is on the same element