【发布时间】:2016-05-24 15:10:07
【问题描述】:
我正在尝试在单击时为某些图像设置特定的 css 类。
这是我的代码:
<%= image_tag ('snapchat.png'), class: "img-rounded", id: "icon-hover", width: "50px" %>
<%= image_tag ('tinder.png'), class: "img-rounded", id: "icon-hover", width: "50px" %>
<%= image_tag ('tumblr.png'), class: "img-rounded", id: "icon-hover", width: "50px" %>
还有我的 jquery:
$(document).on('click', '.img-rounded', function (){
$('.img-rounded').css({
'-webkit-filter':'grayscale(0%)',
'filter':'grayscale(0%)',
});
});
这个功能很好用,问题是当我点击一张图片时,它会将css属性赋予所有其他图片。我怎么能一次只定位一个元素?
感谢您的帮助。
编辑:我的图像被包裹在这样的父元素中:
<b href="" class="wechat" id="icon-hover" style="text-decoration:none">
<%= image_tag ('wechat.png'), class: "img-rounded", id: "icon-hover", width: "50px" %>
</b>
因此,即使使用“this”处理程序,它也不会响应:
$(document).on('click', '.img-rounded', function (){
$(this).css({
'-webkit-filter':'grayscale(0%)',
'filter':'grayscale(0%)',
});
});
【问题讨论】:
-
在处理程序中使用
$(this)。$('.img-rounded').css({==>$(this).css({。我建议您使用 CSS 类并使用.addClass('className)方法添加该类。
标签: javascript jquery html css ruby