js在指定元素最前面或最后面插入标签
deom1 在head标签最前面插入css样式
var head = document.head || document.getElementsByTagName("head")[0]; //获取head标签 var style = document.createElement("link"); //新建 style 标签 style.setAttribute("rel", "stylesheet"); //style 添加属性 style.setAttribute("type", "text/css"); //style 添加属性 style.setAttribute("href", "http://www.51zuso.com/index.css"); //style 添加属性 var first=head.firstChild; // 获取head标签的第一个子元素 head.insertBefore(style,first); // 在head标签的第一个子元素,前插入style标签
重点代码
var first=head.firstChild;
head.insertBefore(style,first);
deom2 在html标签最后面插入js
$(document).ready(function(){ //在html标签最后面引入模块js样式 var html = document.html || document.getElementsByTagName("html")[0]; var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.setAttribute("src", "'.$blockJsPath.'"); html.appendChild(script); });
重点代码
html.appendChild(script);