Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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选择类时出现问题_Jquery_Multithreading_Comments_Css Selectors_Each - Fatal编程技术网

使用jquery选择类时出现问题

使用jquery选择类时出现问题,jquery,multithreading,comments,css-selectors,each,Jquery,Multithreading,Comments,Css Selectors,Each,一段时间以来,我一直在努力使这项工作顺利进行。我正在尝试将注释正文缩短为一行,以便进行紧凑的注释预览。我使用jquery.each函数遍历线程注释系统中的每个.cbody(注释体),并将其放置在chead中的cshort div中。它应该可以工作,但我不能在顶行选择cshort类。非常感谢您的帮助。据我所知,我的jquery应该是这样的: $('.cbody').each(function(Ind1){ var str = $(this).text(); $(

一段时间以来,我一直在努力使这项工作顺利进行。我正在尝试将注释正文缩短为一行,以便进行紧凑的注释预览。我使用jquery.each函数遍历线程注释系统中的每个.cbody(注释体),并将其放置在chead中的cshort div中。它应该可以工作,但我不能在顶行选择cshort类。非常感谢您的帮助。据我所知,我的jquery应该是这样的:

$('.cbody').each(function(Ind1){
         var str = $(this).text();
         $(this).parent().siblings('.chead').next('.cshort').html(str); 
    });
});
这是注释的html:

<ul>
<li>
<div id="23">
<a name="23">
</a>
<div class="chead">
<a href="#" class="unord">
<img src="images/plus.gif" border="0">
</a>
<a href="userinfo.php?user=muuuuuuuu">
<img src="include/avatar/n.jpg" border="0">
</a>
<span style="display: none;" class="uname">
usersname
</span>
&nbsp;,

<span class="cshort">
</span>
<span class="votes_up" id="votes_up23">
2
</span>
-
<span class="votes_down" id="votes_down23">
0
</span>
<span class="vote_buttons" id="vote_buttons23">
<a href="javascript:;" class="vote_up" id="23">
</a>
<a href="javascript:;" class="vote_down" id="23">
</a>
</span>
</div>
<div class="cbody">
<br>
The comment text funny lol
</div>
<a style="display: none;" href="#" class="reply" id="23">
Reply
</a>
<a style="display: none;" href="#21" class="cparent">
Parent
</a>
</div>
</li>
</ul>
  • 用户名称 , 2. - 0
    评论文字好笑哈哈

cbody的父项似乎没有兄弟项,因此位

$(this).parent().siblings('.chead')
没有返回任何内容,您很可能也不想要

$(this).parent().find('.chead')

要最直接地到达cshort,请使用

$(this).parent().find('.cshort')
编辑:

HTML的层次结构如下所示:

ul
| li
| | div#23
| | | a
| | | div.chead
| | | div.cbody
| | | ...
  • $(此)
    指的是cbody
  • $(this).parent()
    指的是div#23
  • $(this).parent().sibbines()
    返回
    li
    的所有其他子节点(在示例代码中为空)
  • $(this).sibbines()
    引用div.chead和一些
    a
    元素
由于chead是cbody的兄弟姐妹,选择它的最佳方法是使用

$(this).siblings('.chead')

cbody的父对象似乎没有兄弟,因此位

$(this).parent().siblings('.chead')
没有返回任何内容,您很可能也不想要

$(this).parent().find('.chead')

要最直接地到达cshort,请使用

$(this).parent().find('.cshort')
编辑:

HTML的层次结构如下所示:

ul
| li
| | div#23
| | | a
| | | div.chead
| | | div.cbody
| | | ...
  • $(此)
    指的是cbody
  • $(this).parent()
    指的是div#23
  • $(this).parent().sibbines()
    返回
    li
    的所有其他子节点(在示例代码中为空)
  • $(this).sibbines()
    引用div.chead和一些
    a
    元素
由于chead是cbody的兄弟姐妹,选择它的最佳方法是使用

$(this).siblings('.chead')
尝试更改此选项:

$(this).parent().siblings('.chead').next('.cshort').html(str);
为此:

$(this).siblings('.chead').children('.cshort').html(str);
.chead
.cbody
是兄弟,因此您不需要查看
.cbody
的父项。另外,
.cshort
.chead
的子项,因此您可以查看
.chead
的子项,而不是它的兄弟项(通过
.next
)。

尝试更改此项:

$(this).parent().siblings('.chead').next('.cshort').html(str);
为此:

$(this).siblings('.chead').children('.cshort').html(str);

.chead
.cbody
是兄弟,因此您不需要查看
.cbody
的父项。另外,
.cshort
.chead
的子对象,因此您可以查看
.chead
的子对象,而不是它的兄弟对象(通过
.next
)。

最后一个方法不应该追加吗


.html(str)每次都将替换它,而您只会得到最后一个。

最后一个方法不应该被追加吗


.html(str)每次都会替换它,而您只会得到最后一个。

非常感谢,这就解决了问题。你能解释一下为什么cbody parent div没有兄弟姐妹吗?切德不是兄弟姐妹吗?非常感谢,这就解决了问题。你能解释一下为什么cbody parent div没有兄弟姐妹吗?chead不是兄弟姐妹吗?谢谢,你的方法也很有效,我给了cobbal正确的答案,因为他比你快一点点,但投了你一票。谢谢,你的方法也很有效,我给了cobbal正确的答案,因为他比你快一点点,但投了你一票。