如何选择父级的第一个子节点?

时间:2019-04-15 13:12:21

标签: javascript jquery html

我的HTML结构如下:

<li class="mktFormReq mktField">
    <label>Industry</label>
    <span class="mktInput">
        <div class="jqTransformSelectWrapper" style="z-index: 9; width: 210px;">
            <div>
                <span style="width: 154px;">Consumer Packaged Goods</span>
                <a href="#" class="jqTransformSelectOpen"></a>
            </div>
            <ul style="width: 208px; display: none; visibility: visible;">
                <li style="">
                    <a href="#" index="0" class="">Industry*</a>
                </li>
                <li>
                    <a href="#" index="1">Business Services</a>
                </li>
                <li>
                    <a href="#" index="2" class="selected">Consumer Packaged Goods</a>
                </li>
                <li>
                    <a href="#" index="3">Energy</a>
                </li>
            </ul>
            <select class="mktFormSelect mktFReq jqTransformHidden required" name="Download_Industry__c" id="Download_Industry__c" size="1" tabindex="8" style="">
                <option value="" selected="selected">Industry*</option>
                <option value="Business Services">Business Services</option>
                <option value="Consumer Packaged Goods">Consumer Packaged Goods</option>
                <option value="Energy">Energy</option>
            </select>
        </div>
        <span class="mktFormMsg"></span>
    </span>
</li>

在这里,我必须使用jQuery在first div的范围内选择值。但是,当使用select元素的name或id属性时,即使用#Download_Industry__c时,我必须选择“消费者包装商品”值(顶部的span和{{1} }属性。

我的方法是选择style的父节点,然后选择第一个子节点,然后继续。但是我无法为其编写语法。我尝试过这种方式:

select

但是它不起作用。

2 个答案:

答案 0 :(得分:2)

您当前仅选择父级。 ($('#Download_Country__c').parent().first().html();$('#Download_Country__c').parent().html();相同)

尝试以下方法获得第一个孩子:

$('#Download_Country__c').parent().children().first().html();

答案 1 :(得分:2)

转到父级,然后find您要的span

$("#Download_Country__c").parent().find("div > span");

您也可以使用siblings

$("#Download_Country__c").siblings(":first-child").find("span");