【问题标题】:Problems to get address using onmouse over使用 onmouseover 获取地址的问题
【发布时间】:2015-11-03 15:50:55
【问题描述】:

使用 onmouseover 事件从<a> 标签获取地址值的最佳方法是什么?

<a href="http://facebook.com" onmouseover="">something</a>

【问题讨论】:

标签: javascript jquery get href


【解决方案1】:
<a href="http://facebook.com" onmouseover="alert(this.getAttribute('href'))">something</a>

Working demo.

您可以使用以下代码将此结果存储在变量中:

document.getElementsByTagName('a')[0].onmouseover = function() {
    var hrefValue = this.getAttribute('href');
    alert(hrefValue); // use hrefValue
}

Working demo.

【讨论】:

  • 也许您能说一下如何将这个结果存储在变量中?
【解决方案2】:
<a href="http://facebook.com" onmouseover="alert($(this).text())">something</a>

$(this).text() 获取a 标签之间的文本,alert 弹出带有值的警告框以确保其正常工作。替代方法是 console.log() 值。这种方式不那么麻烦。

【讨论】:

  • 请考虑编辑您的帖子,以添加更多关于您的代码的作用以及它为何能解决问题的说明。一个大部分只包含代码的答案(即使它正在工作)通常不会帮助 OP 理解他们的问题。
猜你喜欢
  • 2011-11-22
  • 2021-09-25
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
  • 2017-11-27
  • 2020-12-12
  • 2011-04-15
相关资源
最近更新 更多