jQuery DOM事件侦听器失败,并显示“未定义$". [英] jQuery DOM event listener failed with "$ is not defined"
问题描述
根据建议在这里,我在Ruby on Rails应用程序中具有以下内联代码:
Following the advice here, I have the following inline code in a Ruby on Rails app:
document.addEventListener("DOMContentLoaded", function(event) {
$("select#graph_line_duration").change(function(){
$("form").submit();
});
$("input#graph_duration").hide();
});
我收到错误$ is not defined
.但是,如果我单击一个链接转到该页面,则该代码可以正常工作,因此我的jquery
资产正在加载,并且有足够的时间.
I am getting an error $ is not defined
. However, if I click on a link to go to the page, then the code works fine, so my jquery
assets are loading, given sufficient time.
推荐答案
尝试一下:
document.addEventListener("load", function(event) { ... })
https://developer.mozilla.org/zh-CN/docs/Web/Events/DOMContentLoaded
当文档完全加载并解析后,无需等待样式表,图像和子帧完成加载,就会触发DOMContentLoaded事件.(加载事件可用于检测页面的完全加载.)
The DOMContentLoaded event is fired when the document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading (the load event can be used to detect a fully-loaded page).
这篇关于jQuery DOM事件侦听器失败,并显示“未定义$".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!