简体   繁体   English

使用Javascript和/或JQuery,如何根据类查找后代元素?

[英]Using Javascript and/or JQuery, how to find descendant element based on class?

I can't modify this HTML: 我无法修改此HTML:

<li data-corners="false" data-shadow="false" data-iconshadow="true" data-rapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c" class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-btn-up-c">
    <div class="ui-btn-inner ui-li">
        <div class="ui-btn-text"> 
            <a class="make-inline ui-link-inherit" href="#"> 
                <h3 class="ui-li-heading"> Attending: 
                </h3> 
            </a> 

            <div class="ui-select">
                <a href="#" role="button" id="undefined-button" aria-haspopup="true" aria-owns="undefined-menu" data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-icon="arrow-d" data-iconpos="right" data-theme="c" data-inline="true" data-mini="false" class="ui-btn ui-btn-up-c ui-btn-inline ui-shadow ui-btn-corner-all ui-fullsize ui-btn-icon-right ui-disabled" aria-disabled="true">
                    <span class="ui-btn-inner ui-btn-corner-all">
                        <span class="ui-btn-text"> Maybe </span>
                        <span class="ui-icon ui-icon-arrow-d ui-icon-shadow">&nbsp;</span>
                    </span>
                </a>
                <select class="attending 310 mobile-selectmenu-disabled ui-state-disabled" data-inline="true" data-native-menu="false" tabindex="-1" disabled="disabled" aria-disabled="true">
                    <option value="2"> Yes </option>
                    <option value="0"> No </option>
                    <option value="1"> Maybe </option>
                </select>
            </div>
        </div>


        <span class="ui-icon ui-icon-arrow-r ui-icon-shadow">&nbsp;</span>


    </div>
</li>

The select tag contains class="attending", which is unique to this particular page, so I'm using that to gain access to this part of the code. select标记包含class =“ attending”,这对于该特定页面是唯一的,因此我正在使用它来访问代码的这一部分。 Eventually, I'm trying to access the last span tag with class="ui-icon-arrow-r", so I can style it. 最终,我尝试使用class =“ ui-icon-arrow-r”访问最后一个span标签,因此可以对其进行样式设置。 I've tried using jQuery like so: 我试过像这样使用jQuery:

$(".attending").parent().find(".ui-icon-arrow-r").css('background-color','yellow');

It doesn't work. 它不起作用。 When I remove the find() part, it selects all of the li elements on the page. 当我删除find()部分时,它将选择页面上的所有li元素。 So, I know that part is working. 所以,我知道那部分正在工作。 But the find part is not and I can't figure out why not. 但是发现部分不是,我不知道为什么不这样做。 How to select this element? 如何选择这个元素?

尝试这个:

$(".attending").closest('div.ui-btn-inner').find(".ui-icon-arrow-r").css('background-color','yellow');

Here is how I would do it: 我将如何做到这一点:

  1. Add a class (.my-li) to your li (because we want a class-based selector, not tag-based one) 在您的li中添加一个类(.my-li)(因为我们需要一个基于类的选择器,而不是基于标签的选择器)

  2. Use jquery parents method: 使用jQuery parents方法:

     $(".attending").parents('.my-li').find(".ui-icon-arrow-r").css('background-color','yellow')​ 

And yep - here is your case resolved: http://jsfiddle.net/Xfj5U/ 是的-这是您解决的情况: http : //jsfiddle.net/Xfj5U/

Update: 更新:

Sorry, I messed that you can not modify html - my bad :( 抱歉,我搞砸了,您无法修改html-我的坏:(

You still can use parents method like this (with tag selector and not modifying html): 您仍然可以使用这样的Parents方法(使用标签选择器而不修改html):

$(".attending").parents('li').find(".ui-icon-arrow-r").css('background-color','yellow')​

http://jsfiddle.net/ETpVL/ http://jsfiddle.net/ETpVL/

您需要使用parent()遍历两次:

$(".attending").parent().parent().find('span:last-child').css( ... )

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

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