Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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
Javascript jquery选择具有名称的子标记_Javascript_Jquery_Html_Jquery Ui_Jquery Selectors - Fatal编程技术网

Javascript jquery选择具有名称的子标记

Javascript jquery选择具有名称的子标记,javascript,jquery,html,jquery-ui,jquery-selectors,Javascript,Jquery,Html,Jquery Ui,Jquery Selectors,在下面显示的html中,我有一个主div作为cxfeeditem feeditem,并且有许多div具有相同的类名和结构。我的问题是对于所有以类名cxfeeditem feeditem开始的div,如何为子类获取值 1.类名称为cxfeeditem feeditem <div class="cxfeeditem feeditem"> <span class="feeditemtext cxfeeditemtext"> This is my blog

在下面显示的html中,我有一个主div作为
cxfeeditem feeditem
,并且
有许多div
具有相同的类名和结构。我的问题是对于所有以类名
cxfeeditem feeditem
开始的div,如何为子类获取值

1.类名称为cxfeeditem feeditem

 <div class="cxfeeditem feeditem">
   <span class="feeditemtext cxfeeditemtext">
      This is my blog
   </span>
   <a class="feeditemtimestamp">Yesterday 2:13PM</a>
   <div class="cxcomments feeditemcomments"> 
     These are my comments
   </div>
   <div class="cxfeeditem1 feeditem1">
     My comments for the comment
   </div>  

</div>
2.类=feeditemtimestamp

3.cxcomments feeditemcomments

4.cxfeeditem feeditem

 <div class="cxfeeditem feeditem">
   <span class="feeditemtext cxfeeditemtext">
      This is my blog
   </span>
   <a class="feeditemtimestamp">Yesterday 2:13PM</a>
   <div class="cxcomments feeditemcomments"> 
     These are my comments
   </div>
   <div class="cxfeeditem1 feeditem1">
     My comments for the comment
   </div>  

</div>
我尝试了以下操作,但返回空值:

$("div.cxfeeditem.feeditem").each(function() {
   alert($(this).children('span.feeditemtext.cxfeeditemtext').html());
   alert($(this).children('a.feeditemtimestamp').html());
   alert($(this).children('div.cxcomments.feeditemcomments').html());
   alert($(this).children('div.cxfeeditem.feeditem').html());
   break;
 });

根据你的职能

alert($(this).children('span.feeditemtext.cxfeeditemtext').html());
这是在类为cxfeeditem的第一个div中给出span的文本值

 <div class="cxfeeditem feeditem">
   <span class="feeditemtext cxfeeditemtext">
      This is my blog
   </span>
   <a class="feeditemtimestamp">Yesterday 2:13PM</a>
   <div class="cxcomments feeditemcomments"> 
     These are my comments
   </div>
   <div class="cxfeeditem1 feeditem1">
     My comments for the comment
   </div>  

</div>

对于第二个div,span不存在,因此为空。

我能看到的最简单的方法是:

​$('.cxfeeditem.feeditem').filter(
    function(){
        return !$(this)
            .parents('.cxfeeditem.feeditem')
            .length​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​;
    }).children().each(
        function(){
            console.log($(this).text().trim());
        });​

filter()
用于确保我们没有访问同一
.cxfeeditem
.feeditem
类的元素,这些类是这些类中最外层元素的子类。这感觉有点混乱,但考虑到您想要的输出,这似乎是最好的方法

在这之后,我们只需记录每个(直接)未被过滤掉的子元素的空白
trim()
-ed
text()


编辑回答评论中OP的问题,如下所示:

<>如果我想添加一个子类名字,我不想考虑所有的标签;例如,如果我只想要
a.feeditemtimstamp
span.feeditemtext.cxfeeditemtext

在这种情况下,您可以使用第二次调用
filter()

或者您可以使用
find()
(并省略
children()
):

参考资料:

  • jQuery:
  • “普通”JavaScript:

你能明确显示你想从这个jQuery中得到什么结果吗?@David Thomas:请看我的编辑……是的,我知道,这个函数是用来获取din中span标记中的文本的,第二个div没有一个跨度div,所以它在第二次循环中运行null警报,如果你看到正确div.cxFoFiTime.FoeIdIt发生两次,我已经改变了第二个div的名字,它仍然是空的。请看HTML中的编辑,如果我想添加一个子类名字,我不想考虑所有标签…如果我只想要a.feeditemtimestamp和span.feeditemtext.cxFeedItemText的值,那么它们都对我有用(Chrome 19/Win XP);您使用的是什么浏览器/平台,您的开发工具的JavaScript控制台是否打开?您不必为此道歉,但我恐怕无法告诉您为什么对我有效的东西对您无效。至于我的问题,你使用的是什么浏览器/平台?