jQuery脚本只能运行一次 [英] jQuery script works only once

查看:153
本文介绍了jQuery脚本只能运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没什么问题,脚本只能运行一次,之后我需要刷新页面以删除喜欢的文章(脚本就是针对此).

I have little problem, script works only once, after that I need to refresh page to remove favorite article (script is for that).

$("a.fav_no").on('click', function () {
            var id = $(this).attr("id");
            $(this).load("{$homepage}/user_action.php?action=fav&id="+ id +"").addClass("fav_yes");
        });

$("a.fav_yes").on('click', function () {
            var id = $(this).attr("id");
            $(this).load("{$homepage}/user_action.php?action=remove_fav&id="+ id +"").removeClass("fav_yes");
        });

在控制台中,单击多次(因此计算在内)后,单击后得到的文章ID为(div),但它无济于事.因此,我现在可以只选择收藏夹,要从收藏夹中删除,我需要刷新,然后再次单击链接以从收藏夹中删除.

In console, I get id of article (div) on click after many times on click (so it count) but it doesn't do anything. So I can right now just favorite, to remove from favorites I need to refresh then to click again on link to remove from favorites.

谢谢!

推荐答案

如果您使用.on()重载,该重载需要3个args,并且在文档上被调用.然后,事件将随着UI元素的来来去去而保持连接.无需每次都重新添加它们.

If you use the .on() overload that takes 3 args and is called on the document. Then the events will remain hooked up as UI elements come and go. No need to re add them every time.

$(document).on("click","a.offsite",function(){.....

$(document).on("click", "a.offsite", function(){ .....

请参见.on()的说明. http://api.jquery.com/live/

See the description of .on() here. http://api.jquery.com/live/

这篇关于jQuery脚本只能运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆