这很丑陋,必须有更好的方法在jQuery中编写它

Osc*_*son 6 javascript jquery traversal

$(this).parent().parent().parent().parent().find('[name=reply_to_id]');
Run Code Online (Sandbox Code Playgroud)

多数民众赞成看起来很愚蠢,但这是我能想到的最佳方式.我试过parentsUntil('li'),但都没有工作,我也尝试过parents('li')closest('li').在jQuery中有什么东西相当于:

$(this).firstParentThatMatchesThis('li').find('[name=reply_to_id]');
Run Code Online (Sandbox Code Playgroud)

如果不是我认为生病尝试将其提交给jQuery核心...

这是我的HTML(很长时间我把它放在pastebin上):http://pastebin.com/FypJ9WGe

努力让JSFiddle进入那里......

Pet*_*olm 5

试试这个:

$(this).parents("li").eq(0).find('[name=reply_to_id]');
Run Code Online (Sandbox Code Playgroud)

示例:http://jsfiddle.net/FvzT9/

但是,也closest应该工作:

$(this).closest("li").find('[name=reply_to_id]');
Run Code Online (Sandbox Code Playgroud)

示例:http://jsfiddle.net/FvzT9/1/