为什么这个“href”是这样的?值在 Opera 中不起作用?

发布于 2024-10-17 21:47:12 字数 516 浏览 5 评论 0 原文

我有以下内容:

<a href="javascript:jQuery('body').css('backgroundColor','red');">Test</a>

在 Chrome 中运行时,它按预期运行并将页面变成红色。然而,在歌剧中我得到:

[对象对象]

仔细检查发现 Opera 认为 javascript:Query('body')... 是某种 URL。我做错了什么? Opera 无法识别 href 属性中的 javascript: 链接吗?

jsFiddle: http://jsfiddle.net/9CZZL/


编辑: 似乎是火狐也有问题...

I have the following:

<a href="javascript:jQuery('body').css('backgroundColor','red');">Test</a>

When run in Chrome, it functions as expected and turns the page red. However, in Opera I get:

[object Object]

Closer inspection reveals that Opera thinks that javascript:Query('body')... is some kind of URL. What am I doing wrong? Doesn't Opera recognize javascript: links in the href attribute?

jsFiddle: http://jsfiddle.net/9CZZL/


Edit: seems to be a Firefox problem too...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

茶花眉 2024-10-24 21:47:13

问题在于 jQuery('body').css('backgroundColor','red') 的返回值是一个对象,某些浏览器将其解释为网页的新内容。要解决此问题,您可以使用 JavaScript void 运算符将其转换为 undefined,FF 和 Opera(以及可能的其他)将按照您的预期进行处理。您会注意到该页面上也描述了此问题,因为它是 void 运算符的主要用例(代码高尔夫除外,其中 void 0 的字符数少于未定义)。

<a href="javascript:void(jQuery('body').css('backgroundColor','red'));">Test</a>

这应该给出预期的结果: http://jsfiddle.net/9CZZL/13/

它应该是指出以这种方式处理点击被认为是不好的做法。相反,建议使用 JavaScript 中的事件处理程序。这有助于分离 Web 应用程序的不同层,从而使将来的调试变得更加容易。

The issue is that the return value of jQuery('body').css('backgroundColor','red') is an object, which some browsers interpret as the new content for the web page. To fix this, you can use the JavaScript void operator to turn it into undefined, which FF and Opera (and potentially others) will handle as you intended. You will notice that this issue is also described on that page, since it's the premier use-case of the void operator (other than code golf where void 0 is less characters than undefined).

<a href="javascript:void(jQuery('body').css('backgroundColor','red'));">Test</a>

This should give the intended result: http://jsfiddle.net/9CZZL/13/

It should be noted that handling clicks this way is considered bad practice. Instead, using event handlers in JavaScript is recommended. This helps separate the different layers of your web app, which in turn will make debugging in the future easier.

春夜浅 2024-10-24 21:47:13

我刚刚进行了谷歌搜索,不,显然 Opera 无法识别“javascript:”href 值。您必须实施 onClick 或类似的解决方案。

I just did a google search and no, apparently Opera does not recognise "javascript:" href values. You would have to implement an onClick or similar solution.

冷︶言冷语的世界 2024-10-24 21:47:13

在 Firefox 中也是如此...不太清楚为什么。但我从来不喜欢将 JavaScript 放在 URL 中...您可以尝试将其放入 onclick 事件中,或者将整个内容拉出来并将其分开:

http://jsfiddle.net/mnbayazit/6d5An/

Does it in Firefox too.... not quite sure why. But I never liked putting JavaScript right in the URL... you could try putting it in the onclick event or pull the whole thing out and separate it:

http://jsfiddle.net/mnbayazit/6d5An/

〃安静 2024-10-24 21:47:13

这些都在 Opera 10.1 中作为 href 值工作:

javascript:void(0)
javascript:alert("Hello")
javascript:window.location.href = 'http://www.google.com'

但您的代码片段却没有。

一个可行的解决方法是:

<a href="#" onclick="$('body').css('background-color','red')">Test</a>

甚至:

<a href="javascript:void(0)" onclick="$('body').css('background-color','red')">Test</a>

...因为这似乎有效。

These all work as href values for me in Opera 10.1:

javascript:void(0)
javascript:alert("Hello")
javascript:window.location.href = 'http://www.google.com'

Your snippet does not though.

A viable work around is:

<a href="#" onclick="$('body').css('background-color','red')">Test</a>

Or even:

<a href="javascript:void(0)" onclick="$('body').css('background-color','red')">Test</a>

...since that seems to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文