parent().next()返回空字符串

parent().next().text() returning empty string

本文关键字:字符串 next parent 返回      更新时间:2023-09-26

你好,我有下面的HTML,它是用$.get 动态生成的

<div class="forum-post">
  <div>
    <span class="forum-title">Test post</span><span class="forum-type label label-forum-Discussion">Discussion</span>
  </div>
  <div class="forum-author">
    By: Jared De La Cruz on Thursday, June 27, 2013 7:17:43 PM
  </div>
  <pre>
    This is a test post
  </pre>
  <div class="btn-group">
    <a class="btn btn-mini btn-primary btn-forum-comment" id=""><i class="icon-comment icon-white"></i> comment</a>
  </div>
  <div class="btn-group">
    <a class="btn btn-mini btn-inverse btn-forum-comment-show" id=""><i class="icon-plus-sign icon-white"></i> show</a>
  </div>
  <div class="forum-comments">
    <div class="forum-author">
      By: Jared De La Cruz on Saturday, June 29, 2013 11:56:29 PM
    </div>
    <pre>
      This is a test comment
    </pre>
  </div>
  <hr style="border-top: 1px dotted #b0b0b0;border-bottom: 0px">
</div>

我有以下代码:

// Button dynamic comment show
$("#forum").delegate(".btn-forum-comment-show", "click", function() {
  $(this).parent().next(".forum-comments").toggle();
  console.log($(this).parent().next(".btn-forum-comment-show").context.text);
  console.log($(this).parent().next(".btn-forum-comment-show").text());
  $(this).parent().next(".btn-forum-comment-show").context.text = 'hide';
});

以下代码返回:

show
(an empty string)

我想根据切换操作更改文本。显示/隐藏以及图标有什么想法吗?提前感谢!

<div class="btn-group">
    <a class="btn btn-mini btn-inverse btn-forum-comment-show" id="51ccf2471238f1cc13000003"><i class="icon-plus-sign icon-white"></i> show</a>
</div>

更新:

<i class="icon-plus-sign icon-white"></i> show应变为开启切换<i class="icon-minus-sign icon-white"></i> hide

更新2:这似乎奏效了。问题是使用.next()而不是.find()

// Button dynamic comment show
$("#forum").delegate(".btn-forum-comment-show", "click", function() {
  $(this).parent().next(".forum-comments").toggle();
  var change = $(this).parent().find(".btn-forum-comment-show").text();
  var show = '<i class="icon-plus-sign icon-white"></i> show';
  var hide = '<i class="icon-minus-sign icon-white"></i> hide';
  if(change == ' show')
  {
    $(this).parent().find(".btn-forum-comment-show").html(hide);
  }
  if(change == ' hide')
  {
    $(this).parent().find(".btn-forum-comment-show").html(show);
  }
});

请将next()更改为find():

$(this).parent().find(".btn-forum-comment-show").text("hide");