我希望能够在javascript'mouseover'中传递'this'。我基本上想从链接(唯一)中获取 href 并将其传递给每个下载按钮。但是我收到错误消息:
undefined 不是对象(计算 'this.getElementsByClassName('downLoad_link')[0].href = x)
对此很陌生,所以我希望有人能帮助我! :)
这是我的代码:
<a onmouseover="this.getElementsByClassName('downLoad_link')[0].href = 'www.example.com'" href="" class="downLoad_link" download>
<button class="download">Download</button>
</a>
编辑:
当我解决这个问题时,"www.example.com">将被变量替换!
好的,所以我的新代码是这样的:
Javascript:
<script type="text/javascript" language="javascript">
var x = this.getElementsByClassName('getDownloadLink')[0].href; //this is not working!
</script>
.HTML:
<a onmouseover="this.getElementsByClassName('downLoad_link')[0].href = x" href="" class="downLoad_link" download>Download</a>
只使用this
而不使用this.getElementsByClassName('downLoad_link')[0]
<a onmouseover="this.href = 'www.example.com'" href="" class="downLoad_link" download>
<button class="download">
Download
</button>
</a>
我想这将回答你的第二个问题。
var x = document.getElementById('getDownloadLink').href; //this is not working!
<a onmouseover="alert(x)" href="random" id="getDownloadLink">Download</a>
在这里,href 的值被放入变量 x 中。例如,我现在将鼠标悬停更改为 alert(x)。