gpt4 book ai didi

javascript - 使用三元运算符切换按钮文本

转载 作者:行者123 更新时间:2023-11-30 07:04:39 25 4
gpt4 key购买 nike

我正在尝试使用三元运算符切换按钮文本,但它不起作用。

<button type="button" id="ter" >Bar</button>

$("#ter").click(function(){
$(this).text() = 'Bar' ? 'column':'Bar';
});

这样可以吗?我做错了什么?我知道还有很多其他方法可以实现这一点,但我只需要知道这种方法是否可行?

最佳答案

你也可以试试这个:

$("#ter").click(function(){
var newText = ($(this).text() == 'Bar') ? 'column':'Bar';
$(this).text(newText);
});

这会检查当前文本是否等于 'Bar',使用 $(this).text() == 'Bar'。如果是,它将变量 newText 设置为“column”(反之亦然)。第二行用新文本替换旧文本。

关于javascript - 使用三元运算符切换按钮文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25828654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com