使用alert()修复JavaScript / jQuery的奇怪问题 [英] Weird problem with JavaScript/jQuery which get fixed using alert()

查看:76
本文介绍了使用alert()修复JavaScript / jQuery的奇怪问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

$('ul.container_12 li ul li div ul li.icon_export').click(function (e) { 
    e.preventDefault();
    var anchorHrefValue2 = $('a', this).attr('href');
    var mycat2 = $(this).parents('li.current').attr('class').split(' ')[0];
    window.location = anchorHrefValue2+"?active="+mycat2;
});

当我点击Firefox中的菜单时,似乎变量 anchorHrefValue2 未定义,如果我在第3行和第4行之间添加 alert('somthing'); anchorHrefValue2 返回右边value和 mycat2 返回值菜单!我不知道它到底在哪里。
两个变量都已正确设置但不能一起设置。

When I click menu in Firefox seems like variable anchorHrefValue2 is undefined, if I add alert('somthing'); between lines 3 and 4 anchorHrefValue2 returns the right value and mycat2 returns the value menu! Which I don't know where the hell it comes from. both variables has been set correctly but not together.

两者都使用 alert(); 但似乎未定义调用行: window.location = ...;

Both return the right value using alert(); but seems to be undefined calling in the line: window.location=...;.

如果任何人都经历过与此有关的任何事情,请分享。我已经在启用JavaScript的不同浏览器中对此进行了测试。

If anyone has experienced anything close to this, please share. I've tested this in different browsers with JavaScript enabled.

顺便说一句,我有另一个这样的功能,用于具有相同目标的父菜单,效果很好(仅限)选择器与第一个选择器不同):

By the way, I have another function like this for parent menus with the same goal which works just fine(only selectors are different with the first one):

$('ul.container_12 li ul li').click(function (e) { 
    e.preventDefault();
    var anchorHrefValue = $('a', this).attr('href'); 
    var mycat = $(this).parent().parent().attr('class').split(' ')[0]; 
    window.location = anchorHrefValue+"?active="+mycat;
});


推荐答案

当我遇到类似奇怪的错误时我添加了一个警告框,结果发现同一事件(失去焦点的div)触发了两个不同的javascripts。 (我想添加警报让一个脚本有机会在另一个脚本被触发之前完成)。所以像CamelCamelCamel所说的异步行为,但即使没有服务器端代码,也可以获得(不需要的)异步行为。希望能帮助别人。

When I had a similar weird bug that went away when I added an alert box, it turned out that the same event (a div losing focus) was triggering two different javascripts. (I guess adding the alert gave one script a chance to complete before the other was triggered). So asynchronous behaviour as CamelCamelCamel said above, but you can get (unwanted) asynchronous behaviour even if there is no server-side code. Hope that helps someone out.

这篇关于使用alert()修复JavaScript / jQuery的奇怪问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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