javascript - 如何在鼠标悬停时显示传递变量的警报

标签 javascript html

这应该很简单......我不确定我在这里做错了什么。在我的 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/

相关文章:

javascript - 编译后的jst模板中的 `this`是什么

javascript - 修改 <defs><g> 中定义并在 <use> 中使用的 SVG 组件

javascript - 从 HTML 复制的电子邮件在 Thunderbird 中不起作用

html - 通过 css 选定的 li 上的背景图像

html - 以编程方式删除 CSS 类文件 - jQuery

javascript - 如何在 while 循环中串联连接 Promise

javascript - 从远程脚本获取用户IP

javascript - 如何在 Odoo 12 的向导中单击取消后返回主页?

html - 是否有用于生成 HTML 的 Emacs Lisp 库?

html - 下载文件