使用 onmouseover 获取地址的问题

Problems to get address using onmouse over

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

<a href="http://facebook.com" onmouseover="">something</a>
<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.

<a href="http://facebook.com" onmouseover="alert($(this).text())">something</a>

$(this).text() 获取 a 标签之间的文本,并且 alert 弹出带有值的警告框以确保它有效。替代方案是 console.log() 值。这种方式不是那么具有侵入性。