parent().next()返回空字符串
parent().next().text() returning empty string
你好,我有下面的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");
相关文章:
- 如何在JavaScript中将字符串转换为函数引用
- jQuery中是否内置了任何字符串格式化函数
- Javascript,有没有一种方法可以将数组写成没有逗号或空格的单个文本字符串
- ng打开空字符串
- 正则表达式在字符串中找到base64
- 如何将字符串值从php页面发送到java脚本页面
- 子字符串/正则表达式以获取字符串中保存的 SRC 值
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 对id以某个字符串开头的元素进行计数
- Javascript逻辑运算符和字符串/数字
- 反向字符串比较
- 如何有效地将游戏数据存储在URL查询字符串中
- 可变大小的JavaScript字符串如何成为基元类型
- 将DOM节点值与字符串Javascript进行比较
- 如何在 Javascript 中将变量的值从字符串转换为整数
- 本地存储中的字符串到字节数组转换
- JavaScript按钮/输入/函数,字符串反转
- 从查询字符串参数推断出正确的数据类型
- 高亮显示与数组字符串一起使用时文本插件中断
- parent().next()返回空字符串