添加js函数
//加载外部js文件
function loadScript(url) {
var script = document.createElement("script");
script.type = 'text/javascript';
script.src = url;
document.body.appendChild(script);
}
loadScript("url");
//内部添加js代码
function loadScriptString(code) {
var script = document.createElement("script");
script.type = 'text/javascript';
try {
script.appendChild(document.createTextNode(code));//标准写法
} catch (ex) {
script.text = code; //兼容ie
}
document.body.appendChild(script)
}
添加css样式函数
//引入外部css函数
function loaodStyles(url) {
var link = document.createElement('link');
link.rel = "stylesheet";
link.type = "text/css";
link.href = url;
var head = document.getElementsByTagName('head')[0];
head.appendChild(link);
}
//内部添加css代码
function loadStyleString(css) {
var style = document.createElement("style");
style.type = 'text/css';
try {
style.appendChild(document.createTextNode(css));//标准写法
} catch (ex) {
style.styleSheet.cssText = css; //兼容ie
}
var head = document.getElementsByTagName("head")[0];
head.appendChild(style);
}