简体   繁体   English

如何选择孩子<div>具有特定类名的 div 元素是使用 jQuery 的 DOM 中具有该类名的最后一个 div?

[英]How to select the child <div> element of a div with a particular class-name that is the last div with that class-name in the DOM using jQuery?

I am trying to select all elements of the last parent div in the DOM, my DOM gets created with same repeating classNames , so I want to perform action on all the childrens of matching classNames in the last parent div.我试图选择 DOM 中最后一个父 div 的所有元素,我的 DOM 是使用相同的重复 classNames 创建的,所以我想对最后一个父 div 中匹配 classNames 的所有子项执行操作。 I want to select all the elements with class viewMoreTextButton of the last parent div ie slick?我想选择最后一个父 div 的类 viewMoreTextButton 的所有元素,即光滑? I am wondering how to do it, do I need to get the last parent div and loop for same className elemnts or I can use some jQuery built in option.我想知道该怎么做,我是否需要为相同的 className 元素获取最后一个父 div 和循环,或者我可以使用一些 jQuery 内置选项。

<div class="slick">
    <div id="panel-123">
        <div class="buttondesign">
            <div class="viewMoreWrapper">
                <div class="viewMoreText viewMoreTextSizing">
                    <h4 class="header">Hello</h4>
                    <p class="number"></p><ul><li>Points<br></li><li>Access<br></li><li>Fee<br></li><li>Warranty<br></li><li>Offers</li></ul><p></p>
                    <span id="otp" class="countdown" style="display:none;"></span>
                </div>
                <div class="textButtonWrapper">
                    <div class="viewMoreTextButton" onclick="doToggleButton(this);" data-viewmore="" data-viewless="">  
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div id="panel-789">
        <div class="buttondesign">
            <div class="viewMoreWrapper">
                <div class="viewMoreText viewMoreTextSizing">
                    <h4 class="header">Hello</h4>
                    <p class="number"></p><ul><li>Points<br></li><li>Access<br></li><li>Fee<br></li><li>Warranty<br></li><li>Offers</li></ul><p></p>
                    <span id="otp" class="countdown" style="display:none;"></span>
                </div>
                <div class="textButtonWrapper">
                    <div class="viewMoreTextButton" onclick="doToggleButton(this);" data-viewmore="" data-viewless="">  
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="panel-456">
        <div class="buttondesign">
            <div class="viewMoreWrapper">
                <div class="viewMoreText viewMoreTextSizing">
                    <h4 class="header">Hello</h4>
                    <p class="number"></p><ul><li>Points<br></li><li>Access<br></li><li>Fee<br></li><li>Warranty<br></li><li>Offers</li></ul><p></p>
                    <span id="otp" class="countdown" style="display:none;"></span>
                </div>
                <div class="textButtonWrapper">
                    <div class="viewMoreTextButton" onclick="doToggleButton(this);" data-viewmore="" data-viewless="">  
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="slick">
    <div class="panel-890">
        <div class="buttondesign">
            <div class="viewMoreWrapper">
                <div class="viewMoreText viewMoreTextSizing">
                    <h4 class="header">Hello</h4>
                    <p class="number"></p><ul><li>Points<br></li><li>Access<br></li><li>Fee<br></li><li>Warranty<br></li><li>Offers</li></ul><p></p>
                    <span id="otp" class="countdown" style="display:none;"></span>
                </div>
                <div class="textButtonWrapper">
                    <div class="viewMoreTextButton" onclick="doToggleButton(this);" data-viewmore="" data-viewless="">  
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="panel-341">
        <div class="buttondesign">
            <div class="viewMoreWrapper">
                <div class="viewMoreText viewMoreTextSizing">
                    <h4 class="header">Hello</h4>
                    <p class="number"></p><ul><li>Points<br></li><li>Access<br></li><li>Fee<br></li><li>Warranty<br></li><li>Offers</li></ul><p></p>
                    <span id="otp" class="countdown" style="display:none;"></span>
                </div>
                <div class="textButtonWrapper">
                    <div class="viewMoreTextButton" onclick="doToggleButton(this);" data-viewmore="" data-viewless="">  
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="panel-256">
        <div class="buttondesign">
            <div class="viewMoreWrapper">
                <div class="viewMoreText viewMoreTextSizing">
                    <h4 class="header">Hello</h4>
                    <p class="number"></p><ul><li>Points<br></li><li>Access<br></li><li>Fee<br></li><li>Warranty<br></li><li>Offers</li></ul><p></p>
                    <span id="otp" class="countdown" style="display:none;"></span>
                </div>
                <div class="textButtonWrapper">
                    <div class="viewMoreTextButton" onclick="doToggleButton(this);" data-viewmore="" data-viewless="">  
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

I'm not sure you are asking your question correctly, but if you simplify it to just the following piece:我不确定您是否正确地提出了您的问题,但是如果您将其简化为以下部分:

  • I want to select all the elements with class 'viewMoreTextButton', and do something with them.我想选择所有具有“viewMoreTextButton”类的元素,并用它们做一些事情。

That would look like this (jQuery Class Selector):这看起来像这样(jQuery 类选择器):

$(document).ready(function(){
  $(".viewMoreTextButton").css("background-color", "yellow");
});

Note: It helps to put some text in the div and try throwing a simple background color change or 1px solid black border css on whatever you are trying to get when testing selectors (so you can see if you are getting what you want).注意:在 div 中放置一些文本并尝试在测试选择器时尝试获得的任何内容上抛出一个简单的背景颜色更改或 1px 纯黑色边框 css 会有所帮助(这样您就可以查看是否获得了您想要的内容)。 This will select all of the elements with the class viewMoreTextButton and give them a yellow background.这将选择类 viewMoreTextButton 的所有元素,并为它们提供黄色背景。

So then to take this a step further and get the divs with the class viewMoreTextButton to do something such as expand and show more text (as the class name suggests), that would look something like:因此,更进一步并使用类 viewMoreTextButton 获取 div 以执行诸如展开和显示更多文本(如类名称所示)之类的操作,这将类似于:

$(document).ready(function(){  
  $(".viewMoreTextButton").on('click', function () {
    alert('do something that shows more text here...');                
  });
});

This will add an on click handler to each element with class viewMoreTextButton that you can use to show more text by changing the css of each respective element on click from none to block or whatever you need.这将为每个元素添加一个点击处理程序,类 viewMoreTextButton 您可以通过将点击时每个相应元素的 css 从无更改为阻止或您需要的任何内容来显示更多文本。

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

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