开发者

How to get a reference to this DOM structure?

If I only have a reference to document.getElementById('btn_test'), how can I get a reference to that first checkbox input element using only Javascript. Maybe a parent.parent? What is a safe way?

<td>
    <input type="checkbox" name="text_rnd_input"/>
    <label>
        <button id="btn_test" type="button"></button&开发者_如何转开发gt;
    </label>
</td>


btn_test.parentNode.parentNode.getElementsByTagName('input')[0]

Take care, if you use *Sibing properties, because they also select text nodes (for example, invisible ones with only whitespace) and are therefore usually not reliable with arbitrary HTML (think of compressing your HTML by removing whitespace).

And to stress the inevitable: With jQuery:

$btn_test.closest('td').children('input')[0]
// or
$btn_test.parent().prev('input')
// or ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜