选择具有特定ID的TD中的所有元素

时间:2016-06-02 17:05:19

标签: javascript jquery html

如何选择ID为td的{​​{1}}中的所有元素?

在我的代码中,第2和第4个元素的2IDs结尾,因此这些元素背景颜色应该变为红色。

2

这是我的方法:

<table>
  <tr>
    <td>
      <input type="text" id="abc|1">
    </td>
  </tr>
  <tr>
    <td>
      <select id="def|2">
        <option>123</option>
        <option>456</option>
        <option>789</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>
      <input type="radio" id="ghi|1" />
    </td>
  </tr>
  <tr>
    <td>
      <input type="text" id="jkl|2">
    </td>
  </tr>
</table>

FIDDLE

3 个答案:

答案 0 :(得分:2)

第一个问题是您的FIDDLE无法正常工作,因为您忘记了包含jQuery。

第二个问题是您的查询。定义选择器时间距很重要。在您的查询中,您有SELECT o.number AS 'OrderNo', s.fullname AS 'CPC Emp', Max(CASE WHEN t.description = 'Loan Package to Lender' THEN Dateadd(hour, -4, t.completeddate) ELSE NULL END) AS 'Loan Pck To Lender', Max(CASE WHEN t.description = 'Recording Audit' THEN Dateadd(hour, -4, t.completeddate) ELSE NULL END) AS 'Recording Audit', Max(CASE WHEN t.description = 'Recorded Docs' THEN Dateadd(hour, -4, rt.requesteddate) ELSE NULL END) AS 'Recorded Docs Requested', Max(CASE WHEN t.description = 'Recorded Docs' THEN Dateadd(hour, -4, t.completeddate) ELSE NULL END) AS 'Recorded Docs Received', Max(CASE WHEN t.description = 'Recorded Docs to Lender' THEN Dateadd(hour, -4, t.completeddate) ELSE NULL END) AS 'Recorded Docs to Lender', Max(CASE WHEN t.description = 'Recorded Docs to Purchaser' THEN Dateadd(hour, -4, t.completeddate) ELSE NULL END) AS 'Recorded Docs to Purchaser', Max(CASE WHEN t.description = 'Title Policy to Lender' THEN Dateadd(hour, -4, t.completeddate) ELSE NULL END) AS 'TP to Lender', Max(CASE WHEN t.description = 'Title Policy to Purchaser' THEN Dateadd(hour, -4, t.completeddate) ELSE NULL END) AS 'TP Purchaser' FROM pf.orderinfo oi INNER JOIN pfm.[order] o ON ( o.rootid# = oi.rootid ) INNER JOIN core.profile op ON ( oi.owningprofileid = op.id ) INNER JOIN zref.orderstatus os ON ( oi.orderstatus = os.id ) INNER JOIN zref.producttype pt ON ( o.producttypeid = pt.id AND pt.id <> '15' ) INNER JOIN pfm.task t ON ( t.rootid# = oi.rootid AND ( t.description IN ( 'Loan Package to Lender', 'Recording Audit', 'Recorded Docs to Lender', 'Recorded Docs to Purchaser', 'Title Policy to Lender', 'Title Policy to Purchaser' , 'Recorded Docs' ) ) ) LEFT OUTER JOIN pfm.requestedtask rt ON ( rt.rootid# = t.rootid# AND rt.id# = t.id# AND rt.lastid# = t.lastid# ) LEFT OUTER JOIN core.securityidentity s ON ( s.id = t.completedbyid ) WHERE ( op.NAME LIKE 'BH104%' -- WNCW Res Profiles Begin. OR op.NAME LIKE 'CL170%' OR op.NAME LIKE 'CM104%' OR op.NAME LIKE 'IN103%' OR op.NAME LIKE 'SF107%' OR op.NAME LIKE 'AW170%' OR op.NAME LIKE 'JW170%' OR op.NAME LIKE 'KH170%' OR op.NAME LIKE 'PM170%' OR op.NAME LIKE 'SC170%' OR op.NAME LIKE 'EC102%' OR op.NAME LIKE 'MT175%' OR op.NAME LIKE 'CU108%' OR op.NAME LIKE 'NF106%' OR op.NAME LIKE 'GA105%' OR op.NAME LIKE 'SL105%' OR op.NAME LIKE 'PC135%' OR op.NAME LIKE 'PE103%' OR op.NAME LIKE 'OC130%' OR op.NAME LIKE 'WF130%' OR op.NAME LIKE 'SS100%' OR op.NAME LIKE 'SO191%' OR op.NAME LIKE 'CA115%' OR op.NAME LIKE 'DO115%' OR op.NAME LIKE 'WC115%' OR op.NAME LIKE 'WO115%' --WNCW RES Profiles End. AND op.enabled = 1 AND oi.istemplate = 0 AND oi.number NOT LIKE '%test%' AND oi.number NOT LIKE 'tr%' ) AND oi.number = 'WC115-13-0048' AND s.fullname IS NOT NULL GROUP BY s.fullname, o.number ORDER BY 2 DESC 。由于td:nth-child(1)[id$=2]td:nth-child(1)之间没有空格,因此您基本上正在寻找具有特定ID的[id$=2]。你想要的是找到 td中的输入元素。这更合适td - 注意两者之间的空间。该空格表示您要搜索td:nth-child(1) [id$=2]所有后代。如果您只想搜索直接子项,可以使用td来表示>

您也可以针对此特定方案简化查询。如果您希望所有输入的ID都以td:nth-child(1) > [id$=2]结尾,那么您可以使用jquery 2伪选择器来定位它们,如下所示:

:input

https://api.jquery.com/input-selector/

  

描述:选择所有输入,textarea,select和按钮元素。

答案 1 :(得分:1)

使用此jQuery

$("table td:nth-child(1) [id$=2]").css("background-color", "red");

JSFiddle:https://jsfiddle.net/torjpv9t/3/

注意:您只需使用$("[id$=2]")作为选择器。

答案 2 :(得分:-1)

jQuery Selector: Id Ends With?

http://api.jquery.com/all-selector/

请看一下这个链接..

如果您知道元素类型,那么:(例如:将'element'替换为'div')

 $("element[id$='txtTitle']")

在开头使用星号'*'

   $('*[id*=mytext]:...
像这样