j查询如何选择孩子一级div



我有一点问题,目标一些div's没有类,位于表结构:

<table class="blog" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td valign="top">
        <div>               <--- I want to target this
          <table class="some_table_class">
            <tbody>
              <tr>
                <td class="someclass" valign="top">
                   <div>This div should stay untouched, and every of his potential div children</div>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
        <div></div>      <--- I want to target this
        <div></div>      <--- I want to target this
        <div></div>      <--- I want to target this
         .
         .
         .
      </td>
    </tr>
  </tbody>
</table>

我试着在CSS .blog td[valign="top"]>div中这样做但这个也影响了第二个div +似乎IE不理解[valign="top"]是什么

所以我想用jQuery为div添加一些类

有什么有效的方法来做到这一点吗?

.blog>tbody>tr>td>div…有点奇怪,用起来很长,我觉得

谢谢你的建议

我忘了…我可以用jquery编辑html结构,我不能访问。php/。html文件

try this

 $('td[valign="top"]').find("div") // this will find all the divs

要么你用类选择器命名所有div,然后用类选择器得到它。

或者您可以使用not()来排除…但是需要给出一个应该被排除的id或类…

<div id="notthis">This div should stay untouched, and every    
Jquery

$('td[valign="top"]').find("div:not('#notthis')").addClass('test');

小提琴中的简单例子

注释后更新

$('td[valign="top"]').find("div:not('.someclass div')").addClass('test')

在我看来,你应该更多地依赖于你自己设置的类,而不是html属性。

如果您确定您的标记,请尝试$('table.blog>tbody>tr>td>div')。但是,如果我是你,我会在目标项附近添加一个类,以便编写一个更简单的选择器。

最新更新