我正在尝试使用 jquery 和 php 创建一个添加/删除收藏夹。addfavorite 函数有效,但是当我将链接的 id 属性从 addfavorite 更改为 removefavorite 时,它不起作用。id 改变了,但它仍然在做 addfavorite 功能。如果我打开 firebug,我看到 ID 在 id="removefavorite" 中发生了变化,但是当我打开页面源时它没有更改 ID='addfavorite'
我可以用 jquery 更改链接的 ID,即使在 dom 中?
JQUERY:
$(document).ready(function() {
$('#addfavorite').click(function() {
id = $('#item').attr('value');
$.ajax({
type: "POST",
url: "http://127.0.1.1/zend/fm/public/video/addfavorite",
data: "id_video="+id,
cache: false,
async: false,
success: function(result) {
$('#addfavorite').attr('id','removefavorite');
$('#removefavorite').text('Remove from favorite');
getRating(text);
},
error: function(result) {
alert("some error occured, please try again later");
}
});
});
$('#removefavorite').click(function() {
id = $('#item').attr('value');
$.ajax({
type: "POST",
url: "http://127.0.1.1/zend/fm/public/video/removefavorite",
data: "id_video="+id,
cache: false,
async: false,
success: function(result) {
$('#removefavorite').attr('id','removefavorite');
$('#removefavorite').text('Remove from favorite');
getRating(text);
},
error: function(result) {
alert("some error occured, please try again later");
}
});
});
)};
HTML:
<a href="javascript:void(0)" id="addfavorite">
Add to favorite
</a>