【发布时间】:2015-11-03 15:50:55
【问题描述】:
使用 onmouseover 事件从<a> 标签获取地址值的最佳方法是什么?
<a href="http://facebook.com" onmouseover="">something</a>
【问题讨论】:
标签: javascript jquery get href
使用 onmouseover 事件从<a> 标签获取地址值的最佳方法是什么?
<a href="http://facebook.com" onmouseover="">something</a>
【问题讨论】:
标签: javascript jquery get href
<a href="http://facebook.com" onmouseover="alert(this.getAttribute('href'))">something</a>
您可以使用以下代码将此结果存储在变量中:
document.getElementsByTagName('a')[0].onmouseover = function() {
var hrefValue = this.getAttribute('href');
alert(hrefValue); // use hrefValue
}
【讨论】:
<a href="http://facebook.com" onmouseover="alert($(this).text())">something</a>
$(this).text() 获取a 标签之间的文本,alert 弹出带有值的警告框以确保其正常工作。替代方法是 console.log() 值。这种方式不那么麻烦。
【讨论】: