0

如果您加载我的网站并且您没有$_SESSION(您没有登录),我会发送给您<head>

<script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/requires-signup.js"></script>
<script src="/js/core.js"></script>

我的core.js文件包含整个站点的 javascript 代码。例如:

$(document).on('click', '.ajaxload-tab', function(){
   console.log('I shall do the load!');
   return false;
});

但是,对于未登录的用户,这前面是requires-signup.js文件,内容如下:

$(document).on('click', '.requires-signup', function(){
   console.log('No can do!');
   return false;
});

现在,我为您提供这个元素:

<div class="ajaxload-tab requires-signup">CLICK</div>

您单击此元素。你的控制台说:I shall do the load!为什么?

编辑

如果我删除事件绑定return false;中的部分,代码将按预期工作。.ajaxload-tab但是,我不能删除那部分,因为那样会弄乱其他东西。

4

2 回答 2

1

也许尝试像这样在你的 requires-signup.js 中使用 .off()

在 core.js 中:

$(document).on('click.requires-signup', '.ajaxload-tab', function(){
   console.log('I shall do the load!');
   return false;
});

在 requires-signup.js 中

$(document).off(".requires-signup")

http://api.jquery.com/off/

编辑:或者这个方法,如果你不介意全局变量

window.notSignedUp = false;

$(document).on('click.requires-signup', '.ajaxload-tab', function(){
   if (window.notSignedUp) return;
   console.log('I shall do the load!');
   return false;
});

然后在 requires-signup.js 中设置 window.notSignedUp = true

于 2013-11-14T19:26:14.703 回答
0

Try changing the order of the files that you are loading in your head tag, something like this:

<script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/core.js"></script>
<script src="/js/requires-signup.js"></script>

I think your problem is that you are first setting the "click" event to the element in order to console.log 'No can do!', and then, when you load the core.js, you override that "click" event with a new function.

Nevertheless, I recomend working with two separate elements, and display/hide them as you need.

于 2013-11-14T19:20:39.413 回答