在我的代碼中,我將link
的class
從user
更改爲red
。這個想法是,與紅色班級的鏈接不應該單擊工作。但它仍然如此。爲什麼會發生?我正要使用live
,但讀取它已過時,我應該使用on
。jQuery .on在類更改後不能使用鏈接
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test</title>
<script type="text/javascript" src="http://localhost/site/scripts/jQuery.js"></script>
<script type="text/css">
.red{
color:red;
}
</script>
<script type="text/javascript">
$(document).ready(function() {
$(".user").on("click",function() {
alert('Was Clicked');
var idPri = $(this).data("id");
var idSec = $(this).data("sec");
$('.user[data-id="'+idPri+'"][data-sec="'+idSec+'"]').removeClass('user').addClass('red');
$('.h2[data-id="'+idSec+'"]').find('span[data-sec="'+idSec+'"].user').remove();
});
return false;
});
</script>
</head>
<body>
<div id="container">
<div class="h2" data-id="2">Who is your favorite Math teacher?
<div>* User Name <span class="user" data-id="3" data-sec="2">Like</span></div>
<div>* User Name <span class="user" data-id="4" data-sec="2">Like</span></div>
<div>* User Name <span class="user" data-id="5" data-sec="2">Like</span></div>
<div>* User Name <span class="user" data-id="6" data-sec="2">Like</span></div>
<div>* User Name <span class="user" data-id="7" data-sec="2">Like</span></div>
</div>
</div>
</body>
</html>
我可以使用'.user'代替'#容器'嗎?我需要這種方式... – jmenezes
我嘗試使用'活力',它可以正常工作,我貼的代碼。使用「live」有什麼問題? – jmenezes
@jmenezes:不,#容器是包含你的'.user'元素的父節點。 –