0

这应该很简单......我不确定我在这里做错了什么。在我的 HTML 中,我有

<a onmouseover="test(this.id)" id="ok">test me</a>

在我的标题中,我有这个脚本。

<script>
    function test(this.id){
        alert(id);
    }
</script>
4

3 回答 3

3

函数参数名称必须是有效的标识符,而不是表达式:

FormalParameterList :
    标识符
    FormalParameterList ,标识符

this.id是一个表达式,因为它是一个属性访问器,因此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>
于 2013-03-11T21:09:09.480 回答
2

这:

<script>
    function test(id){
        alert(id);
    }
</script>
于 2013-03-11T21:08:57.900 回答
1

试试这个:

<a onmouseover="test(this.id)" id="ok">test me</a>

<script>
    function test(id){
        alert(id);
    }
</script>

演示

于 2013-03-11T21:09:59.590 回答