[英]How can I change the text of a <span> element?
I have a toggle button that I'm adding to an iFrame: 我有一个切换按钮,我正在添加到iFrame:
<script>
$('#list div').each(function(){
$(this).append('<span class="togglebutton">Maximize</span>');
});
$("span.togglebutton").click(function() {
$(this).closest("li").children("div").toggleClass("maximized");
});
</script>
How can I change the toggle between the text Maximize/Minimize when the toggleClass is maximized? 当toggleClass最大化时,如何更改文本Maximize / Minimize之间的切换?
$("span.togglebutton").click(function() {
var $this = $(this); // cache jquerized 'this'
$this.closest("li").children("div").toggleClass("maximized");
var currentText = $this.text();
// if the text is currently "Maximize"
if (currentText === "Maximize") {
// change it to new value
$this.text("New Text");
}
else {
// change it back to "Maximize"
$this.text("Maximize");
}
});
Well, if you are looking for some function like toogleClass()
to do that job for you, you are out of luck. 好吧,如果你正在寻找像
toogleClass()
这样的功能为你做这项工作,那你toogleClass()
走运了。 AFAIK, you have to do it manually. AFAIK,您必须手动完成。
Do something like this 做这样的事情
function toggleText() {
var curText = $("span.togglebutton").text();
var newText;
if(curText=="Maximize") { newText = "Minimize"; }
else { newText = "Maximize"; }
$("span.togglebutton").text(newText);
}
Now you can call it happily where you want to toggle it. 现在,您可以快乐地调用它来切换它。 Like
喜欢
$("span.togglebutton").click(function() {
$(this).closest("li").children("div").toggleClass("maximized");
toggleText();
});
$(this).text('New Text');
OR 要么
$(this).val('New Text');
Determine state. 确定状态。
if ($this.text() == 'Maximized') {
$(this).text('Minimized');
} else {
$(this).text('Maximized');
}
$("span.togglebutton").toggle(function() {
$(this).closest("li").children("div").addClass("maximized")
$(this).text("Maximized Text");
},function(){
$(this).closest("li").children("div").removeClass("maximized")
$(this).text("Minimized Text");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.