Javascript 通过javascript向链接添加鼠标悬停
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4873582/
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
adding a mouseover to a link through javascript
提问by David
Simple quick question....
简单的快速问题....
I have the following link html:
我有以下链接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:
我有一个 javascript 函数,我想动态地将一些 onmouseover 信息输入到该链接中。因此,如果调用此 javascript 函数,则可以说它会变成这样:
<a href="http://www.site.com/" onmouseover="alert('howdy')" />
any ideas how to do this?
任何想法如何做到这一点?
采纳答案by David
Answer was, using setAttribute() javascript.
答案是,使用 setAttribute() javascript。
回答by RameshVel
Add name attribute to and assign onmouseover
将 name 属性添加到并分配 onmouseover
<a href="http://www.site.com/" onmouseover="" name="xxx"/>
document.getelementsbyname('xxx').onmouseover = function() { alert('howdy') }
回答by Nelles
The following works for jQuery every time
以下每次都适用于 jQuery
first the javascript:
首先是javascript:
$(document).on('mouseenter','.hovLink', function (e) {
e.preventDefault();
e.stopPropagation();
alert('entering ' + e.target.id);
}).on('mouseleave','.hovLink', function (e) {
alert('exiting ' + e.target.id);
});
and here is the HTML
这是 HTML
<a href="/link" class="hovLink" id="link1">Link</a>
回答by Manish Trivedi
I think you want to say: dynamically change your href attribute information then you can do it by jquery
我想你想说:动态改变你的href属性信息然后你可以通过jquery来做
//Write code for prompt box and get value (when mouse-over)
$("a[href='http://www.google.com/']").attr('href', 'YOUR_GET_VALUE')
回答by toddles2000
If you can use jquery, see: http://api.jquery.com/hover/
如果可以使用jquery,请参见: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.
这比直接更改属性要好。您的 javascript 函数可以动态绑定/取消绑定鼠标悬停事件并执行您的警报调用。
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.
否则,您的 javascript 函数将需要动态更改属性,但您需要解决浏览器差异以找到正确的元素,然后找到并修改 onmouseover 属性。
回答by Mallox
two options:
两个选项:
if it's something small:
如果是小东西:
<a href="http://www.site.com/" onmouseover="this.href = 'http://stackoverflow.com'" />
if you have something more to do:
如果您还有其他事情要做:
<script type="text/javascript">
function doSomething(elem) {
elem.href = 'http://stackoverflow.com';
}
</script>
<a href="http://www.site.com/" onmouseover="doSomething(this)">test</a>
Or as stated before: use jQuery or any other framework to make your life a lot easier
或者如前所述:使用 jQuery 或任何其他框架让你的生活更轻松