通过jquery选择具有动态id的div的child

通过jquery选择具有动态id的div的child,jquery,jquery-selectors,Jquery,Jquery Selectors,看看这些家伙: <div id="Item_23"> <div class="Info"> <a class="Link" href="#" onclick="call(23)">Link</a> <div class="text"> <a class="cancel" href="cancel.asp">Cancel</a> </div> </div> &

看看这些家伙:

<div id="Item_23">
 <div class="Info">
   <a class="Link" href="#" onclick="call(23)">Link</a>
   <div class="text">
     <a class="cancel" href="cancel.asp">Cancel</a>
   </div>
 </div>
</div>

首先,修复原始选择器,如下所示:

 var item = $('div[id=Item_' + e + '] div');
有许多方法可以选择子元素,包括和,但这是最简单的方法,也最不可能因标记更改而中断:

item.find('.Link');
也许像

$('div[id^=Item_] div.text');

响应您的编辑

试试这个

function call(e) {

    // Find the inner anchor
    var theAnchor = $('div[id=Item_' + e + '] div.text').find('a.cancel');
}
调用或jQuery代码中缺少下划线,因此它正在查找ID为的div

Item23
而不是身份证

Item_23

下面是一个JSFIDLE,它演示了在单击另一个链接时获取cancel链接

正如@undefined所建议的,您不需要内联javascript调用,因为您使用的是jQuery。我对函数做了一些修改,因此它只获取父对象的ID,并将整个ID作为参数发送

HTML

<div id="Item_23">
    <div class="Info">
        <a class="Link" href="#">Link</a>
        <div class="text">
            <a class="cancel" href="cancel.asp" data-val="Yay got it!">Cancel</a>
        </div>
    </div>
</div>

父div具有动态id,其数字部分是可变的。如果
e
参数中有下划线,则函数应该可以工作。否则,将其设置为
$('div[id=Item_'+e+']div.text)。fadeOut()
是的,它只需要一个下划线。好的,如果我的div.text有子项,如何选择它?将它放在您的标记中以便我们可以看到,然后解释您试图使用它做什么。看到我的答案,它会选择。取消指定的项id。哦,天哪,我很抱歉,写得不好,请查看我与SHYThank一起编辑的代码,非常感谢James,请原谅我的错误:)
Item_23
<div id="Item_23">
    <div class="Info">
        <a class="Link" href="#">Link</a>
        <div class="text">
            <a class="cancel" href="cancel.asp" data-val="Yay got it!">Cancel</a>
        </div>
    </div>
</div>
$(document).ready(function(){
    $('a.Link').click(function(){
        var id = $(this).parents('div[id^=Item_]').attr('id');
        callAnchor(id);
    });
});

function callAnchor(e) {

    // Find the inner anchor
    var theAnchor = $('div[id=' + e + '] div.Info div.text').find('a.cancel');

    // Print the data-val attribute to prove that it was found.
    alert($(theAnchor).attr('data-val'));
}