如何使用jquery定位输入

时间:2014-07-18 08:52:18

标签: jquery jquery-selectors

我有一张桌子,其中有各种各样的行。在每一行中,第一列包含一个包含输入的范围。

 <tr>
      <td>
           <select></select>
           <span class="comboSpan">
                <input title class="custom textbox"> 
           </span>
      </td>
 </tr>

enter image description here

如何使用jquery更改输入文本颜色?我的部分屏幕如下:

 $("#tTable tbody tr").each(function () {
      var td = $(this).children('td:first');
      var span = td.children('span:first');                       
      span.children().find(':input').addClass("redColor");
 });

2 个答案:

答案 0 :(得分:1)

span.children().find(':input')行造成了问题。因为span的直接子元素是输入元素。而且你不需要遍历所有的tr元素。

尝试,

$("#tTable tbody tr td:first-child span:first-of-type input").addClass("redColor");

DEMO

答案 1 :(得分:0)

您可以像下面一样更正您的脚本:

$("#tTable tbody tr").each(function () {
      var td = $(this).children('td:first');
      var span = td.find('span:first');// change                       
      span.find('input').addClass("redColor");// change 
 });

OR

您可以使用以下代码:

 $(function(){
      $("#tTable tbody tr td:first").find('span input').addClass('redColor');
    });