【发布时间】:2012-07-15 16:09:25
【问题描述】:
我想在单击按钮时更改它。最初只有一个“编辑”按钮。单击它后,它将变成一个“保存”按钮,我还想在它旁边显示一个“取消”按钮。 我怎样才能做到这一点?我有下面的代码。
<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>demo by roXon</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<button data-text="Save">Edit</button>
<p>Hello</p>
<p style="display: none">Good Bye</p>
<script>
$("button").click(function(){
$(this).nextUntil('button').toggle();
var btnText = $(this).text();
$(this).text( $(this).data('text') );
$(this).data('text', btnText );
});
</script>
</body>
</html>
【问题讨论】:
-
为什么不简单地拥有三个带有 id 的按钮(编辑、保存、取消),然后根据需要更改它们,而不是使用段落和数据功能?
-
@j08691 你有例子我可以看看吗?
-
@j08691 好一个!但是,如果我在一页上有 3 组这些按钮呢?我需要给他们不同的 id 并为他们单独编写脚本吗?对不起,我是 jquery 新手...
标签: javascript jquery html button toggle