[英]jQuery closest not traversing past parent
I'm after an input that's inside 3 divs
, a td
, and a tr
. 我需要输入3
divs
, td
和tr
。 There are no usable unique attributes to search with (id's and rows change frequently). 没有可用的可用唯一属性来搜索(ID和行经常更改)。 I'm trying to traverse the DOM by starting with a
label
in a div
in an adjacent td
, go to the next td
, then down through a div
a div
another div
then into the input
. 我试图用一个开始遍历DOM
label
的div
在相邻的td
,转到next td
,然后向下一div
一div
的另一个div
然后进入input
。
I started with $('[title*="P2P"]').closest('td').next('.control-cell').find('input');
我从
$('[title*="P2P"]').closest('td').next('.control-cell').find('input');
It doesn't select the input for me, so I started working backwards by printing the class of the selected element 1 step at a time, but it breaks as soon as use closest
to go past the first div
. 它没有为我选择输入,因此我通过每次一次打印所选元素的类开始向后工作,但只要使用
closest
第一个div
,它就会中断。
My jsfiddle: https://jsfiddle.net/28kacj28/ 我的jsfiddle: https ://jsfiddle.net/28kacj28/
And my HTML: 而我的HTML:
<tr class="control">
<td class="label-cell left-label">
<div class="workitemlabel">
<label class="workitemcontrol-label" for="witc_135_txt" title="[Field Name: Process Start Date]">Start Date:</label>
</div>
</td>
<td class="control-cell">
<div class="workitemcontrol">
<div id="witc_135" class="combo input-text-box date-time drop">
<div class="wrap">
<input type="text" id="witc_135_txt" autocomplete="off" maxlength="255" title="7/7/2016 12:00:00 AM" aria-invalid="false">
</div>
<div class="drop bowtie-calendar bowtie-icon"></div>
</div>
</div>
</td>
</tr>
<tr class="control">
<td class="label-cell left-label">
<div class="workitemlabel">
<label class="workitemcontrol-label" for="witc_136_txt" title="[Field Name: P2P]">P2P:</label>
</div>
</td>
<td class="control-cell">
<div class="workitemcontrol">
<div id="witc_136" class="combo input-text-box date-time drop">
<div class="wrap">
<input type="text" id="witc_136_txt" autocomplete="off" maxlength="255" title="7/29/2016 12:00:00 AM" aria-invalid="false">
</div>
<div class="drop bowtie-calendar bowtie-icon"></div>
</div>
</div>
</td>
</tr>
<span></span>
I'm not exactly sure what you are trying to select but you made two mistakes: 我不确定您要选择什么,但是您犯了两个错误:
<tr>
, <td>
) with a table. <tr>
, <td>
)。 Browsers will remove tr
and td
tags automatically and dynamically (meaning "View Source..." will still show the tags even though they were removed) when they are not properly wrapped by a table
tag. table
标签未正确包装,浏览器将自动动态地删除tr
和td
标签(这意味着即使已删除标签,“ View Source ...”仍将显示标签)。 This means the tr.control
tag was not being found. tr.control
标记。 testClass
was being assigned from the undefined (misspelled) variable testShit
instead of testStuff
(line 2). testClass
是通过未定义(拼写错误)变量testShit
而不是testStuff
(第2行)分配的。 Keep it classy. I made corrections in this fiddle: https://jsfiddle.net/28kacj28/3/ 我在此提琴中进行了更正: https : //jsfiddle.net/28kacj28/3/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.