简体   繁体   English

jQuery最接近而不穿越父级

[英]jQuery closest not traversing past parent

I'm after an input that's inside 3 divs , a td , and a tr . 我需要输入3 divstdtr 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 labeldiv在相邻的td ,转到next td ,然后向下一divdiv的另一个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: 我不确定您要选择什么,但是您犯了两个错误:

  1. Wrap table elements ( <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标签未正确包装,浏览器将自动动态地删除trtd标签(这意味着即使已删除标签,“ View Source ...”仍将显示标签)。 This means the tr.control tag was not being found. 这意味着tr.control标记。
  2. Your class variable 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM