简体   繁体   English

使用Jquery选择一个td元素

[英]Select a td element using Jquery

I would like to go through my table's 'td's, and check for a data attribute and do something according to that attribute. 我想通过我的表'td',检查数据属性并根据该属性执行某些操作。 I did this: 我这样做了:

<li id="person1" data-city="Boston, New York, San Fransisco">
    Person name 1
</li>
<li id="person1" data-city="down, Washington">
    Person name 2
</li>
<td data-city="down"> TEST </td>

$('li[data-city*="down"]').css('color','red');​

http://jsfiddle.net/fR8rJ/3/ http://jsfiddle.net/fR8rJ/3/

but I can't make it work for the 'td' element. 但是我不能让它适用于'td'元素。

any ideas? 有任何想法吗?

You can't have an orphan td , it's not valid HTML, and jQuery will not allow you to select it. 你不能拥有孤儿td ,它不是有效的HTML,jQuery也不允许你选择它。 It needs to be inside a tr , which itself needs to be inside a table element: 它需要在tr ,它本身需要在table元素中:

<table>
  <tr>
    <td data-city="down"> TEST </td>
  </tr>
</table>

Only then you can execute $('td[data-city*="down"]').css('color','red'); 只有这样你才能执行$('td[data-city*="down"]').css('color','red'); .

DEMO . 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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