【发布时间】:2010-10-22 05:34:52
【问题描述】:
以下小sn-p代码一旦部署就无法更改(它位于RIA中),因此必须通过bootstrapper.js文件加载所有内容:
<div id="someDivID"></div>
<script type="text/javascript" src="bootstrapper.js"></script>
加载所有 js、css 和标记的最佳方式是什么?有没有比以下更好(更干净、更快、更清晰)的方法?:
function createDivs() {
var jsDiv = document.createElement('div');
jsDiv.id = 'allJavascriptHere';
var contentDiv = document.createElement('div');
contentDiv.id = 'allContentHere';
document.getElementById("someDivID").appendChild(jsDiv);
document.getElementById("someDivID").appendChild(contentDiv);
function importScript(url){
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
document.getElementById("jsDiv").appendChild(script);
}
importScript("http://example.com/jquery-1.4.2.min.js");
importScript("http://example.com/anotherScript.js");
}
window.onload = function(){
$.get("http://example.com/someHTML.html", function(data) {
$('#contentDiv').html(data);
setTimeout("javaScript.init()", 200);
});
}
someHTML.html 文件中的样式表如下:
<style type="text/css">
@import url("example.com/styleSheet.css");
</style>
(注意:我不知道为什么需要setTimeout,但出于某种原因我需要。也许你的答案不需要它。)
【问题讨论】:
标签: javascript jquery html css