开发者

Weird problem with JavaScript/jQuery which get fixed using alert()

Here's my code:

$('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;
});

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.

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

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 j开发者_如何学运维ust 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;
});


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.


ul.container_12 li ul li div ul li.icon_export

This is a very long and very specific selector, if you change your HTML code only a little, this selector will break. Try simplifying it. To me it seems that .container_12 .icon_export would work as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜