【发布时间】:2016-02-06 01:21:22
【问题描述】:
我有一个简单的页面,其中有四张卡片(Div),每张卡片都有一个按钮。我写了一些 jquery,希望当我点击按钮时,我可以更改按钮的类和文本......并在我继续点击它们时在两种状态之间切换。
我下面的代码似乎完美地适用于第一个按钮......但不是其他任何一个。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#un_btn").click(function() {
if ($(this).attr('class') == 'btn_s') {
$(this).html('Undo?');
$(this).removeClass('btn_s');
$(this).addClass('notsu');
} else {
$(this).html('Mark as not suitable?');
$(this).removeClass('notsu');
$(this).addClass('btn_s');
}
});
});
</script>
</head>
<body>
<div class="card">
<p>Card 1</p>
<button class="btn_s" id="un_btn">Mark as not suitable?</button>
</div>
<div class="card">
<p>Card 2</p>
<button class="btn_s" id="un_btn">Mark as not suitable?</button>
</div>
<div class="card">
<p>Card 3</p>
<button class="btn_s" id="un_btn">Mark as not suitable?</button>
</div>
<div class="card">
<p>Card 4</p>
<button class="btn_s" id="un_btn">Mark as not suitable?</button>
</div>
</body>
</html>
我做错了什么?我怎样才能让切换在每个相应的 div(卡)中的每个按钮上工作?
【问题讨论】: