通过 JavaScript 添加鼠标悬停到链接

发布于 2024-10-15 19:13:18 字数 331 浏览 10 评论 0原文

简单的快速问题......

我有以下链接 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一江春梦 2024-10-22 19:13:18

添加 name 属性并分配 onmouseover

<a href="http://www.site.com/" onmouseover="" name="xxx"/> 
document.getelementsbyname('xxx').onmouseover = function() { alert('howdy') } 

Add name attribute to and assign onmouseover

<a href="http://www.site.com/" onmouseover="" name="xxx"/> 
document.getelementsbyname('xxx').onmouseover = function() { alert('howdy') } 
2024-10-22 19:13:18

答案是,使用 setAttribute() javascript。

Answer was, using setAttribute() javascript.

无风消散 2024-10-22 19:13:18

我想你想说的是:动态改变你的href属性信息然后你可以通过jquery来做到这一点

//Write code for prompt box and get value (when mouse-over)
$("a[href='http://www.google.com/']").attr('href', 'YOUR_GET_VALUE')

I think you want to say: dynamically change your href attribute information then you can do it by jquery

//Write code for prompt box and get value (when mouse-over)
$("a[href='http://www.google.com/']").attr('href', 'YOUR_GET_VALUE')
浅紫色的梦幻 2024-10-22 19:13:18

如果您可以使用 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.

别再吹冷风 2024-10-22 19:13:18

两个选择:

如果事情很小:

<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 或任何其他框架让您的生活变得更轻松

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

浅听莫相离 2024-10-22 19:13:18

以下内容每次都适用于 jQuery

首先使用 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);
  });

这里是 HTML

<a href="/link" class="hovLink" id="link1">Link</a>

The following works for jQuery every time

first the 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

<a href="/link" class="hovLink" id="link1">Link</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文