【发布时间】:2019-03-23 02:25:01
【问题描述】:
var $ = function (id) {
"use strict";
return document.getElementById(id);
};
$(document).ready(function () {
"use strict";
$('redirect').click(function () {
window.alert('you clicked the link');
return false;
});
});
<a href="http://www.google.com" id="redirect">Visit Goggle</a>
我有一个 HTML 链接,我想在我的其余代码中使用 JavaScript 和 Jquery。当用户单击链接时,我希望弹出一条带有消息的警报,但我不希望链接将他们重定向到该页面。当我使用开发人员工具在 Chrome 中查看我的代码时,我收到一条错误消息,上面写着“Uncaught TypeError: cannot read property of 'ready' of null”。为什么 ready 属性为 null,我该如何解决?
var $ = function (id) {
"use strict";
return document.getElementById(id);
};
$(document).ready (function () {
"use strict";
$('redirect').click(function () {
window.alert('you clicked the link');
return false;
});
});
<a href="http://www.google.com" id="redirect">Visit Goggle</a>
`
【问题讨论】:
-
您正在尝试重新分配 jQuery 的
$- 不要这样做。还要确保您使用的是 jQuery:<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
标签: javascript jquery html function