【发布时间】:2019-04-06 18:14:07
【问题描述】:
我创建了一个带有按钮的 HTML 文件。有一个 JavaScript 文件,其中包含在按下按钮时向浏览器开发者控制台打印消息的代码。
在 Chrome 中一切正常,但在 Firefox 中却不行
关于将“事件”作为参数显式传递给与操作关联的函数,我在 SO 上遇到了一些问题,我已经这样做了,但仍然没有收到 Firefox 的响应。
这是我的 JS
$("document").ready(function(){
console.log('JS loaded');
$("i.fa-trash-alt").click(function(event){
console.log('Delete icon pressed');
});
})
这是我的 HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
<title>Playground</title>
</head>
<body>
<div class="container-fluid">
<button type="button" class="btn btn-warning"><i class="fa fa-trash-alt"></i></button>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="test.js"></script>
</body>
</html>
如果我遗漏了某些步骤或是否需要任何其他信息,请告知。
更新 - 解决方法
作为一种解决方法,我将“删除”事件链接到按钮而不是图标。这似乎在 Firefox 中有效。
【问题讨论】:
-
$("document")应该是$(document)。document是一个全局窗口对象 -
感谢@charlietfl 的提示