Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery如何选择子级第一级div';s_Jquery_Css_Target - Fatal编程技术网

jQuery如何选择子级第一级div';s

jQuery如何选择子级第一级div';s,jquery,css,target,Jquery,Css,Target,我有一个小问题,针对一些没有类的div,位于表结构中: <table class="blog" cellspacing="0" cellpadding="0"> <tbody> <tr> <td valign="top"> <div> <--- I want to target this <table class="some_table_

我有一个小问题,针对一些没有类的div,位于表结构中:

<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>


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

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

试试这个

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

要么使用类选择器命名所有div,然后使用类选择器获取该div

或者您可以使用
not()
来排除该问题。。但需要提供一个应该排除的id或类

HTML

<div id="notthis">This div should stay untouched, and every    
中的简单示例

评论后更新

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

为您的div分配类或ID`s,您可以使用$(“#mydivID”)或$(“.mydivClass”)轻松地对它们进行寻址是的,这就是为什么我要针对这些div添加一些类,我不愿意在.php文件中这样做:/OK,如果您无法修改初始标记,我理解这是一个有意义的实践,有一点客户端处理来增强它。。然后在增强标记的基础上构建更稳定的代码。这也会影响
我想我只想针对第一级表中的那些div选择器匹配第一级和嵌套的
td
=>第一行将返回两次不需要的div,而第二行将返回一次。你应该为这些div添加一个类看起来不错。。。我发现
$('td[valign=“top”]').find(“div:not('someclass div'))).addClass('test')也可以工作,也许是更好的解决方案,以防#not这将包含一些其他div。非常感谢:)是的。。你明白了…:)。。该死我错过了。。无论如何。。很高兴它有帮助。。。。我也会以同样的方式更新答案。。谢谢。。快乐编码
$('td[valign="top"]').find("div:not('.someclass div')").addClass('test')