Javascript 内置引导图标的Jquery切换文本按钮

Javascript 内置引导图标的Jquery切换文本按钮,javascript,jquery,html,twitter-bootstrap,twitter-bootstrap-3,Javascript,Jquery,Html,Twitter Bootstrap,Twitter Bootstrap 3,我正在使用Boostrap 3和Jquery,我正在尝试用Boostrap图标范围切换按钮内的文本,但不知道怎么做。这就是我尝试的:html代码: <button type='button' class='btn btn-success btn-md'> <span class='glyphicon glyphicon-edit'></span> Edit</button> 编辑 Jquery代码: $(this).text(functio

我正在使用Boostrap 3和Jquery,我正在尝试用Boostrap图标范围切换按钮内的文本,但不知道怎么做。这就是我尝试的:
html代码:

<button type='button' class='btn btn-success btn-md'>
<span class='glyphicon glyphicon-edit'></span> Edit</button>

编辑

Jquery代码:

$(this).text(function(i, text){
  return text === "<span class='glyphicon glyphicon-edit'></span> Close" ? "<span class='glyphicon glyphicon-edit'></span> Edit" : "<span class='glyphicon glyphicon-edit'></span> Close"
})
$(this).text(函数(i,text){
返回文本==“关闭”?“编辑”:“关闭”
})
那么……这是怎么回事?
1) 使用切换文本按钮但保持相同图标的示例
2) 另一个使用切换文本按钮但切换图标类的示例
谢谢。

您应该使用
html()
而不是
text()

$(this).html(函数(i,文本){
返回文本==“关闭”?“编辑”:“关闭”
})
您应该使用
html()
而不是
text()

$(this).html(函数(i,文本){
返回文本==“关闭”?“编辑”:“关闭”
})
这个怎么样:

<button class="btn btn-primary toggle-text" data-toggle="collapse" href="#collapseExample">
<span class='glyphicon glyphicon-edit'></span><span>Edit</span><span class="hidden">Close</span>...
这个怎么样:

<button class="btn btn-primary toggle-text" data-toggle="collapse" href="#collapseExample">
<span class='glyphicon glyphicon-edit'></span><span>Edit</span><span class="hidden">Close</span>...

您可以使用而不是=,因为:

就像你可以看到的,有一个额外的字符(换行符),你不能依赖它

jQuery.html的代码段是:

$('button')。在('click',函数(e){
$(this).html(函数(i,文本){
返回(text.indexOf(“关闭”)!=-1)?“编辑”:“关闭”
})
})

编辑
而不是=您可以使用,因为:

就像你可以看到的,有一个额外的字符(换行符),你不能依赖它

jQuery.html的代码段是:

$('button')。在('click',函数(e){
$(this).html(函数(i,文本){
返回(text.indexOf(“关闭”)!=-1)?“编辑”:“关闭”
})
})


编辑
由于要将原始HTML放入元素中,因此需要使用
.HTML
而不是
.text
。最后,我将“编辑”文本放入标记中,这样更容易替换内容。但是谢谢你的回答。因为你正在将原始HTML放入元素中,所以你需要使用
.HTML
而不是
.text
。最后我将“编辑”文本放入一个标记中,这样更容易替换内容。但是谢谢你的回答。
$('.hidden').removeClass('hidden').hide();
$('.toggle-text').click(function() {
    $(this).find('span').each(function() { $(this).toggle(); });
});
$(this).text() === "↵     Edit"