[英]Find specific div on click and toggle class
这是我当前的标记:
.image_container{style: 'background-image: url(' + item['image'] + ')'}
[...]
.product_images_link{data: {id: item["objectId"]}}
%form.product_info{action: "post", id: "item_#{item['objectId']}"}
%h3.title= item['name']
%a.remove
我有一个CoffeeScript函数,可在a.remove上切换类“ .add”。 我想单击此链接还将“未选中”的类切换为“ .image_container”。
我当前的函数如下所示:
toggleProduct: (evt) ->
button = $(evt.currentTarget)
button.toggleClass 'add'
$(evt.currentTarget).find('.image_container').toggleClass 'unselected'
如何在.image_container上查找和切换类?
.image_container
是按钮的子级吗? 如果是这样,您的代码应该可以工作。 这是我认为您要实现的目标的一种变体:
如果它不在按钮内,则只需使用其他jQuery搜索来找到它:
如您所见,我正在使用$button.parent().find('img')
。
您还可以为其分配一个ID,并根据该按钮具有的某些属性来对其进行切换:
我向按钮添加了一个data-toggle
属性,其id的值是您想要切换的值,然后使用$('#' + $button.attr('data-toggle'))
选择器搜索该元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.