在下面的代码中,我将class
a link
from更改user
为red
。这个想法是红色类的链接不应该在点击时起作用。但它仍然存在。为什么会这样?我正要使用live
,但读到它已被弃用,我应该使用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>