注意事项:
我无权访问HTML(由引擎呈现)
jQuery包含在引擎中(这就是我不在代码中包含它的原因)
此代码接受链接并将其注入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将datepicker放入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不是函数
感谢您的帮助!
答案 0 :(得分:1)
自从我查看将脚本手动加载到DOM中的状态已经有几年了,但是当脚本实际加载完成时,通常会发生loaded
事件的变种。
一般来说,这个过程有一些异步,因为它必须去获取文件。这意味着他们可能不会在发生这种情况时停止执行javascript引擎。这意味着在某种形式的时尚中你会看到一个回调。
这样做有各种各样的库。如果您不想使用它们,请阅读来源并调查您要定位的平台的要求。
这样的东西:
看起来jquery甚至有某种形式:https://api.jquery.com/jquery.getscript/