<img title="<a href='#' onClick='alert('Hello World!')>The Link</a>" /> 中的 JavaScript 可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4283614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript inside an <img title="<a href='#' onClick='alert('Hello World!')>The Link</a>" /> possible?
提问by Tomkay
<img title="<a href='#' onClick='alert('Hello World!')>The Link</a>" />
So I've got some specific question.. I already know that I can work with attributes inside a TITLE attribute.. But can I work with events inside a TITLE attribute?
所以我有一些具体的问题.. 我已经知道我可以使用 TITLE 属性内的属性.. 但是我可以使用 TITLE 属性内的事件吗?
(btw - It seems like a rubbish code, but this already works as is should on my web project - I just need a way to use some Javascript on this sheathed link.)
(顺便说一句 - 这似乎是一个垃圾代码,但这已经在我的网络项目中正常工作了 - 我只需要一种在这个带护套的链接上使用一些 Javascript 的方法。)
I USE Jqueryframework.
我使用Jquery框架。
回答by Piskvor left the building
No, this is, as you say "rubbish code". If it works as should, it is because browsers try to "read the writer's mind" - in other words, they have algorithms to try to make sense of "rubbish code", guess at the probable intent and internally change it into something that actually makes sense.
不,正如你所说的“垃圾代码”。如果它正常工作,那是因为浏览器试图“读懂作者的想法”——换句话说,他们有算法来尝试理解“垃圾代码”,猜测可能的意图并在内部将其更改为实际的内容说得通。
In other words, your code only works by accident, and probably not in all browsers.
换句话说,您的代码只是偶然运行,并且可能不适用于所有浏览器。
Is this what you're trying to do?
这是你想要做的吗?
<a href="#" onClick="alert('Hello World!')"><img title="The Link" /></a>
回答by Naim Zard
When you click on the image you'll get the alert:
当您单击图像时,您将收到警报:
<img src="logo1.jpg" onClick='alert("Hello World!")'/>
if this is what you want.
如果这是你想要的。
回答by joni
Im my browser, this doesn't work at all. The tooltip field doesn't show a link, but <a href='#' onClick='alert('Hello World!')>The Link</a>
.
I'm using FF 3.6.12.
我的浏览器,这根本不起作用。工具提示字段不显示链接,而是<a href='#' onClick='alert('Hello World!')>The Link</a>
。我正在使用 FF 3.6.12。
You'll have to do this by hand with JS and CSS. Begin here
您必须使用 JS 和 CSS 手动完成此操作。从这里开始
回答by Sean Patrick Floyd
no, you can't do that, but you can use event handlers to change the title:
不,您不能这样做,但您可以使用事件处理程序来更改标题:
<img src="foo.jpg" onmouseover="this.title='it is now ' + new Date()" />
回答by ICTMitchell
<img title="<a href="javascript:alert('hello world')">The Link</a>" />