通过 JavaScript 添加鼠标悬停到链接
简单的快速问题......
我有以下链接 html:
<a href="http://www.site.com/" onmouseover="" />
我有一个 javascript 函数,我想在该链接中动态输入一些 onmouseover 信息。那么,假设如果调用这个 javascript 函数,它就会变成这样:
<a href="http://www.site.com/" onmouseover="alert('howdy')" />
有什么想法如何做到这一点吗?
Simple quick question....
I have the following link html:
<a href="http://www.site.com/" onmouseover="" />
I have a javascript function which I want to enter some onmouseover information into that link dynamically. So, lets say it then becomes this for example if this javascript function is called:
<a href="http://www.site.com/" onmouseover="alert('howdy')" />
any ideas how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
添加 name 属性并分配 onmouseover
Add name attribute to and assign onmouseover
答案是,使用 setAttribute() javascript。
Answer was, using setAttribute() javascript.
我想你想说的是:动态改变你的href属性信息然后你可以通过jquery来做到这一点
I think you want to say: dynamically change your href attribute information then you can do it by jquery
如果您可以使用 jquery,请参阅: http://api.jquery.com/hover/
这是比直接改变属性要好。您的 JavaScript 函数可以动态绑定/取消绑定鼠标悬停事件并执行警报调用。
否则,您的 javascript 函数将需要动态更改属性,但您需要解决浏览器差异以找到正确的元素,然后找到并修改 onmouseover 属性。
If you can use jquery, see: http://api.jquery.com/hover/
This is better than changing the attribute directly. Your javascript function can dynamically bind/unbind the mouse hover event and execute your alert call.
Otherwise your javascript function will need to dynamically change the attribute but you'll need to work around browser differences to locate the correct element then locate and modify the onmouseover attribute.
两个选择:
如果事情很小:
如果您还有更多事情要做:
或者如前所述: 使用 jQuery 或任何其他框架让您的生活变得更轻松
two options:
if it's something small:
if you have something more to do:
Or as stated before: use jQuery or any other framework to make your life a lot easier
首先使用 javascript:
这里是 HTML
first the javascript:
and here is the HTML