我试图使用jQuery在每次点击时切换按钮的标签。
以下是我实现的代码:
<html>
<head>
<script src="jquery-1.8.2.js"></script>
<script>
$(document).ready(function(){
$("#details_button").click(function() {
var button_text = $("#details_button").attr("text");
button_text == "Hide" ? $("#details_button").text("Details") : $("#details_button").text("Hide");
});
});
</script>
</head>
<body>
<button id="details_button" type="button">Details</button>
</body>
</html>我希望“details_button”中的标签每次单击时都会更改,从“Details”更改为“Hide”,反之亦然。
但它只更改一次,从“Details”更改为“Hide”,然后在以后的单击中不会更改。
我在这里错过了什么?
发布于 2012-10-08 02:08:15
将$("#details_button").attr("text");更改为$("#details_button").text();
发布于 2012-10-08 02:01:13
试一试:http://jsfiddle.net/rsnSx/ 或 http://jsfiddle.net/z7mrs/ 或 http://jsfiddle.net/dNzrq/
所用API:
.html() - http://api.jquery.com/html/
.on - http://api.jquery.com/on/
希望它符合:)的事业
码
$(document).on('click','#details_button',function() {
var button_text = $(this).html();
button_text == "Hide" ? $(this).text("Details") : $(this).text("Hide");;
});发布于 2012-10-08 02:01:31
这应该能行
$("#details_button").html() == "Hide" ? $("#details_button").html("Details") : $("#details_button").html("Hide");;https://stackoverflow.com/questions/12774453
复制相似问题