jQuery 如何替换 jQueryUI 按钮文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2665937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I replace jQueryUI button text?
提问by Andrei R
I've got a button that I use with jQueryUI somethink like this (simplified).
我有一个与 jQueryUI 一起使用的按钮,就像这样(简化)。
<button id="mybutton">Play<button>
<script>
$("#mybutton").button().toggle(
function(){
$(this).text('Stop');
},
function(){
$(this).text('Start');
},
);
</script>
This code breaks the way the button looks because when making it into the button widget, there's a new span added inside the button. So I'm changing the button value like this now
这段代码打破了按钮的外观,因为在将其放入按钮小部件时,按钮内添加了一个新的跨度。所以我现在像这样更改按钮值
$(this).find('span').text('Stop');
This is hacky because I can't treat the button as a black box anymore and have to go inside.
这是 hacky,因为我不能再把按钮当作一个黑盒子,必须进去。
Is there a clean way to do this?
有没有干净的方法来做到这一点?
回答by gnarf
Maybe you could use the label
option of the jQuery UI button now instead?
也许您现在可以改用label
jQuery UI 按钮的选项?
$("#mybutton").button().toggle(function() {
$(this).button('option', 'label', 'Stop');
}, function() {
$(this).button('option', 'label', 'Start');
});
回答by ajpiano
You can use the .button("refresh") method after you alter the text.
您可以在更改文本后使用 .button("refresh") 方法。
$("#mybutton").button().toggle( function(){ $(this).text('Stop').button("refresh"); }, function(){ $(this).text('Start').button("refresh"); }, );
回答by mikehoncho
after calling .button() to make a jQuery UI button, the text seems to be removed from the original button element and placed in a <span class = ui-button-text></span>
within the button element.
在调用 .button() 制作 jQuery UI 按钮后,文本似乎从原始按钮元素中移除并放置在<span class = ui-button-text></span>
按钮元素内的a中。
Something like this would work:
像这样的事情会起作用:
$("#myButton > .ui-button-text").text("New Text");
回答by Stephen O'Flynn
I don't know if you're still looking for an answer to this, but I found your question. If you look at the code from the button, jQuery adds a tag or two. So instead of rewriting the entire thing, I replaced the button .html()with the same html but used javascript search()to switch out the label. It's ugly, but it works. It also uses the button label to switch correctly.
我不知道你是否还在寻找这个问题的答案,但我找到了你的问题。如果您查看按钮中的代码,jQuery 会添加一两个标签。因此,我没有重写整个内容,而是将按钮.html()替换为相同的 html,但使用 javascript search()来切换标签。这很丑陋,但它有效。它还使用按钮标签来正确切换。
var bdef = $(this).html();
var ht = "";
if (bdef.search("Nuevo") != -1) {
ht = $(this).html();
$(this).html(ht.replace("Nuevo", "Lista"));
}
else {
ht = $(this).html();
$(this).html(ht.replace("Lista", "Nuevo"));
}
回答by user1464277
You need to do .button("refresh")
你需要做 .button("refresh")
$('#mybutton').text('Save').button("refresh");
回答by psychotik
Is the id of the button always myButton
? If yes, then just use
按钮的 id 总是myButton
吗?如果是,那么只需使用
$("#myButton").text('Stop');
$("#myButton").text('Stop');