注意事项:
我无权访问 HTML(由引擎呈现)
jQuery 包含在引擎中(这就是为什么我不将它包含在我的代码中)
此代码获取一个链接并将其注入(inject)到 html 中:
$(document).ready(function(){
addSS('https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css');
addScript('https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js');
addScript('//InjectedHTML.js');
});
function addScript(str){
var script = document.createElement('script');
script.src = str;
document.head.appendChild(script);
};
function addSS(str){
var link = document.createElement('link');
link.rel= 'stylesheet';
link.href = str;
link.type= 'text/css';
document.head.appendChild(link);
};
InjectedHTML 将日期选择器放入 HTML 中(在与上述代码不同的单独文件中):
$(document).ready(function() {
addClass(); //adds class attribute to an input that allows me to use datepicker function
datePicker(); //adds calendar
});
function addClass(){
$("#DateWrapper input").attr("class", "datepicker");
};
function datePicker(){
$( ".datepicker" ).datepicker();
};
收到此错误:
InjectedHTML.js:35 Uncaught TypeError: $(...).datepicker 不是函数
感谢您的帮助!
最佳答案
自从我研究手动将脚本加载到 DOM 中的状态以来,已经有几年了,但是通常有一个 loaded
事件的变体,当脚本实际完成加载时会触发该事件.
通常,该过程存在一些异步情况,因为它必须去获取文件。这意味着在这种情况发生时他们可能不会停止 JavaScript 引擎的执行。这意味着以某种形式,您正在查看回调。
有各种库可以执行此操作。如果您不想使用它们,请阅读源代码并调查您所针对的平台的要求。
像这样的东西:
- https://github.com/ded/script.js/blob/master/src/script.js
- https://github.com/rgrove/lazyload/blob/master/lazyload.js
- 请注意,这些天这些都是一毛钱一打
看起来 jquery 甚至有某种形式:https://api.jquery.com/jquery.getscript/
关于javascript - 获取 typeerror 函数不存在错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37749448/