我一直在敲打我的头撞墙,试图使这项工作一段时间了。我试图缩短的评论体一线为紧凑的评论。我使用的!每个功能要经过的每一个。cbody(评论体)在我的螺纹评论的系统,并将其放置在cshort div内部chead.它应工作,但我不能选择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>
有帮助吗?

解决方案

该cbody的父似乎没有兄弟姐妹,所以位

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

是不回任何东西,你最有可能想要的

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

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

得到最直接的cshort,使用

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

编辑:

你HTML的层次看起来是这样的:

ul
| li
| | div#23
| | | a
| | | div.chead
| | | div.cbody
| | | ...
  • $(this) 指cbody
  • $(this).parent() 指div#23
  • $(this).parent().siblings() 返回的所有其他儿童节点的 li (empty在你的样本代码的情况下)
  • $(this).siblings() 指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)。

应该不是最后的方法来附加?

的.html(STR)将每次替换它,你会只与最后一个结束。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top