Javascript内联显示跨浏览器的差异?
我有以下内联 Javascript 代码:
<a href="javascript:{ document['example'].src = 'cube.png'; document.getElementById('constructor').innerHTML = 'Mesh mesh = new Mesh.Cube();'; }">Cube</a>
对于您可怜的疲惫的程序员眼睛,这里是扩展版本:
document['example'].src = 'cube.png';
document.getElementById('constructor').innerHTML = 'Mesh mesh = new Mesh.Cube();';
此代码充当超链接,将 example
图像更改为 3D 立方体的图像并更改
的内容到适当的构造函数。 (这显然是一个教程页面)。
这在 Chrome 中工作得很好,但在其他浏览器中,我要么得到一个新页面,要么整个页面的内容更改为:
Mesh mesh = new Mesh.Cube();
代码有什么问题?令我困惑的是,它在浏览器中有效,而在另一个浏览器中无效。就好像脚本找不到“构造函数”元素并建议将整个页面作为后备。我远不是 Javascript 专家,所以这只是一个疯狂的猜测。
I have the following inline Javascript code:
<a href="javascript:{ document['example'].src = 'cube.png'; document.getElementById('constructor').innerHTML = 'Mesh mesh = new Mesh.Cube();'; }">Cube</a>
For your poor tired programmer eyes, here's the expanded version:
document['example'].src = 'cube.png';
document.getElementById('constructor').innerHTML = 'Mesh mesh = new Mesh.Cube();';
This code acts as a hyperlink that changes the example
image to an image of a 3D cube and changes a <pre id="constructor">
's content to the appropriate constructor. (This is obviously a tutorial page).
This works perfectly fine in Chrome, but in other browsers, I get either a new page or the whole page's content changed to:
Mesh mesh = new Mesh.Cube();
What is the problem with the code? What puzzles me is that it's valid in a browser and not in another. It's as if the script couldn't find the 'constructor' element and proposed the whole page as a fallback. I'm far from being a Javascript expert, so that's just a wild guess.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我必须说我从未在锚链接中看到过这种符号,我的意思是使用小括号在其中放入一些代码。
我在 Chrome 中尝试过,它确实有效,但在 FireFox 中无效。
不过,您可能想这样尝试:
但说实话,我只是创建一个辅助函数并直接调用它,例如:
或者类似的东西(更好的是动态地将事件侦听器附加到锚链接)
Well I must say I've never seen this kind of notation in an anchor link, using the braket to put some code in it I mean.
I tried in Chrome, it did work indeed, but not in FireFox.
You may want to try like that though:
But to be honest I would just create an helper function and call it directly like:
Or something like that (even better would be to dynamically attach an event listener to the anchor link)
试试这个:
在你的 JavaScript 代码中:
Try this:
In your JavaScript code:
我只会回答“代码有什么问题?”
href
的行为与任何 onXXX 事件不同(因此是 javascript: 协议)。它尝试加载新文档并在其中放入一些内容。最糟糕的是,它捕获了所有输出。因此,为了使其按原样工作,您需要捕获所有语句值作为赋值:当然,所有这些都在
javascript:{...}
中。这里还有一些很好的评论和解释: http://www.west-wind.com /weblog/posts/9899.aspx
I will just answer "What is the problem with the code?"
The
href
behaves differently than any onXXX event (hence javascript: protocol). It kind of tries to load new document and put something inside. The worst thing, it catches all output. So, to make it work as-is, you need to catch all statement values as assignments:all in
javascript:{...}
of course.Also some good comments and explanations here: http://www.west-wind.com/weblog/posts/9899.aspx