为什么更改链接 ID 后执行相同的功能?
我正在尝试使用 jquery 和 php 创建一个添加/删除收藏夹。 addfavorite 函数可以工作,但是当我将链接的 id 属性从 addfavorite 更改为 removefavorite 时,它不起作用。 id 改变了,但它仍然执行 addfavorite 功能。 如果我打开firebug,我会看到id =“removefavorite”中的ID发生了变化,但是当我打开页面源时,它没有更改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>
I'm trying to create a add/remove favorite with jquery and php.
The addfavorite function works but when i change the id attribute of the link from addfavorite to removefavorite it doesn't work. The id changed but it still doing addfavorite function.
If i open firebug i see that the ID changed in id="removefavorite", but when i open the page source it didn't change ID='addfavorite'
Ho can i change with jquery the ID of a link even in the 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,您将使用多少个使用 ID 属性的链接?请记住,ID 仅适用于单个元素。
我建议您使用切换函数 http://api.jquery.com/toggle-event 来执行此操作/ 。
我猜代码不起作用,因为您使用了 .click 事件而不是 .live 事件,并且当您将其绑定到 click 事件时,#removefavorite 事件不存在(因为它是 #removefavorite)。还要记住,事件被分配给元素,因此元素可以更改属性并仍然保留事件。如果您不想使用切换或实时功能来执行此操作,则可以在分配另一个单击事件之前取消绑定第一个单击事件。
另外,Firebug 会动态显示修改后的 DOM,这就是为什么您可以看到它的更改。当您执行查看源代码时,您正在查看下载的原始 HTML 文档(没有任何 javascript 对其进行更改)。
我还建议您使用 Firefox 的 FireQuery 扩展:它会向您显示该元素的绑定事件(如果您使用 jQuery 进行开发,还会显示其他有用的数据)
希望我的第一篇文章对您有所帮助:)
First of all, how many links using the ID attribute you will be using? Remember that ID is just for a single element.
I would recommend you to do this using a toggle function http://api.jquery.com/toggle-event/ .
I guess the code is not working because you used .click event instead of .live event, and the #removefavorite event does not exist when you bounded it to the click event (because it's #removefavorite). Also remember that events are assigned to elements, so the element can change the attributes and still retain the event. You can unbind the first click event before you assign the other one if you don't want do it with toggle or live functions.
Also, Firebug shows modified DOM on the fly, that's why you can see it changed. When you do a view source, you are viewing the original HTML document that you downloaded (without any javascript changing it).
I also recommend you to use the FireQuery extension for Firefox: It will show you the binded events to that element (amongst other useful data if you are developing in jQuery)
Hope my first post helps you :)
不要更改元素的
id
,只需更改标签即可。我想这会对你有帮助。Don't change
id
of an element just change the label. I think it will help you.好的,答案是您在文档加载后绑定您的点击函数
$(document).ready(function() { ... }
。因此,您确实可以更改链接的 ID,如果Ajax 调用已成功进行,我应该无法发现任何错误。
问题是你没有刷新你的触发器。因此,即使元素上的 ID 已更改,前一个触发器仍与其绑定。
因此,解决该问题的方法是通过重做
$('#removefavorite').click(function() { ... } );
来刷新触发器,为此编写另一个函数,因为您不能相互嵌套 1290383948 次。 ;-)
这是我在 JSFiddle 中快速制作的一个示例: http://jsfiddle.net/S2ERT/
它的编码不正确,因为我在 1 分钟内完成了此操作,但您会明白这个想法。
Ok, the answer is that you bind your click functions after the document has loaded in
$(document).ready(function() { ... }
.So indeed you change your ID of the link if the Ajax call was made successfully, and this should work. I cannot spot any errors in that.
The thing is that you do not refresh your triggers. So even though the ID has changed on the element, the previous trigger is still bound to it.
So the way to solve it is to refresh the triggers by redoing the
$('#removefavorite').click(function() { ... } );
Write another function for that though, because you cannot nest it 1290383948 times in each other. ;-)
Here is an example I quickly made in JSFiddle: http://jsfiddle.net/S2ERT/
It's not properly coded as I did this in 1 minute but you'll catch the idea.