jQuery 脚本在 Firefox 和 Internet Explorer 上返回新窗口
我的页面上有一个元素的切换按钮,当我单击它时,我可以看到脚本运行并切换该元素,但随后我得到一个新窗口,其中返回了 JS 对象[我猜..]。它只是一个空白窗口,其中包含我的 href 网址,上面写着 [object Object]
在 chrome 中没问题,但 ie 和 firefox 就这样做。 他们的方式我这样做是
<a href="javascript:jQuery('#thatDude').toggle();">click</a>
<div id="thatDude">blah</div>
更奇怪的部分是我也有这是我的脚本文件
jQuery('.someBtn').live('click', function() {
jQuery('#someForm').toggle();
});
,当我单击它时 - 它在任何地方都可以正常工作... [是的,我知道我应该将 live 更改为 delegate =] ]
有谁知道是什么发生什么事了? 我使用 jquery 1.6 [但 1.4.4 也是如此..]
非常感谢
i have this toggle button for an element on my page, and when i click it, i can see the script runs and toggles the element, but then i get a new window with the JS object returned [i guess..]. it just a blank window with a url of my href and it says [object Object]
in chrome its fine, but ie and firefox do this.
they way i do this is
<a href="javascript:jQuery('#thatDude').toggle();">click</a>
<div id="thatDude">blah</div>
the even stranger part is that i also have this is my script file
jQuery('.someBtn').live('click', function() {
jQuery('#someForm').toggle();
});
and when i click that - it works fine anywhere... [yeah i know i should change live to delegate =] ]
does anyone know whats going on?
im using jquery 1.6 [but 1.4.4 does the same..]
thanks so much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您的点击事件处理程序返回 false,这应该有帮助。
return false from your click event handler, that should help.
发生的情况是,
javascript:
URI 中脚本的返回值(如果该返回值不为 void)将被视为 HTML 并进行解析。 WebKit 除外,它没有实现该功能(下面提到的一个例外)。在你的例子中,我假设 jQuery 的toggle()
返回一个未定义的实际值,并且该值被视为要解析为 HTML 的字符串。您可以在 IE、Firefox 和 Opera 中看到这个简单测试用例的行为:
这在 WebKit 浏览器中完全失败,尽管这在 WebKit 浏览器中确实有效,因为它们专门破解了一个代码路径:
如果您想确保脚本的返回值是 void,将
void(0)
放在脚本末尾。What's going on is that the return value of the script in a
javascript:
URI, if that return value is not void, is treated as HTML and parsed as such. Except in WebKit, which doesn't implement that feature (with one exception mentioned below). In your case, I assume that jQuery'stoggle()
returns an actual value that's not undefined, and that value is treated as a string to be parsed as HTML.You can see the behavior in this simple testcase in IE and Firefox and Opera:
This totally fails in WebKit browsers, though this does work in WebKit because they special-hack one codepath:
If you want to make sure your script's return value is void, put
void(0)
at the end of the script.