简体   繁体   English

jQuery将:nth-​​child(#)和addClass()应用于div,不会将class添加到元素

[英]jQuery applying :nth-child(#) and addClass() to div, will not add class to element

I have tried many variations of applying the :nth-child , using 1, 2, 3, etc. trying to get a hit on the div I need to add a class to: <div style="width: 304px; height: 78px;"> . 我尝试了使用:nth-child多种变体,使用1、2、3等。试图在div上获得成功,我需要添加一个类: <div style="width: 304px; height: 78px;">

I was able to add a class to the parent div: .g-recaptcha so I know my jQuery is working. 我能够将一个类添加到父div: .g-recaptcha所以我知道我的jQuery正在工作。 I have also tried variations of $('.g-recaptcha :nth-child(1)').addClass('bbbb'); 我还尝试了$('.g-recaptcha :nth-child(1)').addClass('bbbb'); , adding div.g-recaptcha div:nth-child(1) - prefixing with a div but still not luck. ,添加div.g-recaptcha div:nth-child(1) -带有div前缀​​但仍然不走运。

Can you tell me what I am doing wrong or why I cannot get a class added to: <div style="width: 304px; height: 78px;"> ? 您能告诉我我做错了什么,还是为什么我不能添加一个类: <div style="width: 304px; height: 78px;">吗?

jQuery jQuery的

$('.g-recaptcha :nth-child(1)').addClass('bbbb');

HTML HTML

<div class="ginput_container">
    <div class="g-recaptcha" data-sitekey="6Le59BwTAAAAABBcVxJyYHAbAzfFn8D6cLyVdPm9" data-theme="light">
        <div>
            <div style="width: 304px; height: 78px;">
                <iframe src="https://www.google.com/recaptcha/api2/anchor?k=6Le59BwTAAAAABBcVxJyYHAbAzfFn8D6cLyVdPm9&amp;co=aHR0cDovL3d3dy5zaWxraW5tYW5hZ2VtZW50Z3JvdXAuY29tOjgw&amp;hl=en&amp;v=r20160404124926&amp;theme=light&amp;size=normal&amp;cb=xksc4agtgw8z" title="recaptcha widget" width="304" height="78" role="presentation" frameborder="0" scrolling="no" name="undefined"></iframe>

            </div>

            <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid #c1c1c1; margin: 10px 25px; padding: 0px; resize: none;  display: none; "></textarea>

        </div>

With this way , you are sure to target the right div with your code : 通过这种方式 ,您可以确保使用代码定位正确的div

$('div[style="width: 304px; height: 78px;"]').addClass('bbbb');

OR this way : 或者这样:

$('.g-recaptcha div').find('div').addClass('bbbb');

You can try this, 你可以试试看

1) Working Fiddle 1) 工作小提琴

$('.g-recaptcha iframe').parent().addClass('bbbb');

get hold of the iframe inside the div .g-recaptcha and it is to this iframe parent div you need to add the class. 获取div .g-recaptcha内的iframe,并且需要向该iframe父div添加类。 So find the parent of this iframe and add the class. 因此,找到该iframe的父级并添加该类。

This works if you have only 1 iframe inside your .g-recaptcha div, If your HTML structure doesnt support this code let me know... We can improve 如果您的.g-recaptcha div中只有1个iframe,则此方法有效,如果您的HTML结构不支持此代码,请告诉我...我们可以改进

2) Working Fiddle 2) 工作小提琴

  $('.g-recaptcha div:eq(1)').addClass('bbbb');

Use eq() selector to access the div's by index 使用eq()选择器按索引访问div

3) Working Fiddle 3) 工作小提琴

$('.g-recaptcha div div').addClass('bbbb');

Since you have 2 nested div's you can find the inner div like this.. 由于您有2个嵌套的div,因此您可以找到这样的内部div。

Also many more... 还有更多...


Edit 1: As suggested by CupawnTae you can also do this 编辑1:根据CupawnTae的建议,您也可以执行此操作

$('.g-recaptcha iframe[title="recaptcha widget"]').parent().addClass('bbbb');

To target the first div inside a div inside .g-recaptcha you can use 要定位第一div一个内部div.g-recaptcha ,您可以使用

.g-recaptcha>div>div:first-child

Or to target the parent of the element with title="recaptcha widget" , use 或使用title="recaptcha widget"定位元素的父对象,请使用

$('[title="recaptcha widget"]').parent()

Example: 例:

 $('[title="recaptcha widget"]').parent().addClass('bbbb'); 
 .bbbb { background: red; padding: 20px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="ginput_container"> <div class="g-recaptcha" data-sitekey="6Le59BwTAAAAABBcVxJyYHAbAzfFn8D6cLyVdPm9" data-theme="light"><div> <div style="width: 304px; height: 78px;"> <iframe src="https://www.google.com/recaptcha/api2/anchor?k=6Le59BwTAAAAABBcVxJyYHAbAzfFn8D6cLyVdPm9&amp;co=aHR0cDovL3d3dy5zaWxraW5tYW5hZ2VtZW50Z3JvdXAuY29tOjgw&amp;hl=en&amp;v=r20160404124926&amp;theme=light&amp;size=normal&amp;cb=xksc4agtgw8z" title="recaptcha widget" width="304" height="78" role="presentation" frameborder="0" scrolling="no" name="undefined"></iframe> </div> <textarea id="g-recaptcha-response" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid #c1c1c1; margin: 10px 25px; padding: 0px; resize: none; display: none; "></textarea> </div> 

The div you are targeting is a sibling and not a child of g-recaptcha 您定位的div是同级兄弟,而不是g-recaptcha的子g-recaptcha

Use something like 使用类似

$('.g-recaptcha + div').addClass('bbbb');

Or use some selector like siblings() 或使用类似siblings()的选择器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM