这应该很简单......我不确定我在这里做错了什么。在我的 HTML 中,我有
<a onmouseover="test(this.id)" id="ok">test me</a>
在我的标题中,我有这个脚本。
<script>
function test(this.id){
alert(id);
}
</script>
最佳答案
Function parameter names必须是valid identifiers ,不是表达式:
FormalParameterList :
Identifier
FormalParameterList , Identifier
this.id
是一个表达式,因为它是 property accessor ,因此使用 this.id
作为形参名称是一个语法错误。改变
function test(this.id){
alert(id);
}
至
function test(id){
alert(id);
}
// or
function test(foo){
alert(foo);
}
或者简单地说
<a onmouseover="alert(this.id)" id="ok">test me</a>
关于javascript - 如何在鼠标悬停时显示传递变量的警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15348663/