使用jquery将类添加到父div [英] Add class to parent div using jquery

查看:190
本文介绍了使用jquery将类添加到父div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有下面的html结构



这个应该相当容易,但由于某种原因我挣扎着。

 < div class =subcontent_detail> 
< h4>一些文字< / h4>
< div class =divider>< / div>
< div class =changevid>< / div>
< / div>

我想要的是当changevid被点击时,一个名为'active'的类应该被添加到子内容之后

我的代码


$ b $ pre $ $('。changevid')。click(function(e){
e.preventDefault();
$ (this).parent()。parent()。addClass('active');
return false;
});

非常欢迎任何帮助,请提前致谢。

parent()两次。这应该工作:

  $('。 changevid')。click(function(e){
$(this).parent()。addClass('active');
});

请注意,您也不需要 preventDefault()返回false 因为点击的元素不是链接。


This is should be fairly easy but for some reason I am struggling.

I have this following html structure

<div class="subcontent_detail>
  <h4>some text</h4>
  <div class="divider"></div>
  <div class="changevid"></div>
</div>

What I want is when changevid is clicked a class called 'active' should be added after subcontent

my code

$('.changevid').click(function(e){
e.preventDefault();
$(this).parent().parent().addClass('active');
return false;
  });

Any help is highly welcomed. Thanks in advance.

解决方案

You were calling parent() twice. This should work:

$('.changevid').click(function(e){
  $(this).parent().addClass('active');
});

Note you also don't need to preventDefault() or return false because the clicked element is not a link.

这篇关于使用jquery将类添加到父div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆