简单快速的问题....
我有以下链接HTML:
<a href="http://www.site.com/" onmouseover="" />
我有一个JavaScript函数,想要动态地在链接中输入一些onmouseover信息。因此,假设这个JavaScript函数被调用,它可能会变成这样:
<a href="http://www.site.com/" onmouseover="alert('howdy')" />
有什么想法如何做到这一点?
简单快速的问题....
我有以下链接HTML:
<a href="http://www.site.com/" onmouseover="" />
我有一个JavaScript函数,想要动态地在链接中输入一些onmouseover信息。因此,假设这个JavaScript函数被调用,它可能会变成这样:
<a href="http://www.site.com/" onmouseover="alert('howdy')" />
给 <a> 标签添加 name 属性,并赋值 onmouseover
<a href="http://www.site.com/" onmouseover="" name="xxx"/>
document.getelementsbyname('xxx').onmouseover = function() { alert('howdy') }
id
属性。 - Greg B答案是使用JavaScript中的setAttribute()方法。
我认为你想表达的是:动态更改href属性信息,那么你可以使用jQuery来实现。
//Write code for prompt box and get value (when mouse-over)
$("a[href='http://www.google.com/']").attr('href', 'YOUR_GET_VALUE')
两个选项:
如果是一些小东西:
<a href="http://www.site.com/" onmouseover="this.href = 'http://stackoverflow.com'" />
如果你还有其他事情要做:
<script type="text/javascript">
function doSomething(elem) {
elem.href = 'http://stackoverflow.com';
}
</script>
<a href="http://www.site.com/" onmouseover="doSomething(this)">test</a>
或者如之前所述:使用jQuery或任何其他框架可以让您的生活变得更加轻松
$(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);
});
这里是HTML代码
<a href="/link" class="hovLink" id="link1">Link</a>