我正在尝试使用 javascript 动态加载 css 文件,有一段时间我认为它可以工作。但下一刻我意识到,页面上的元素没有从 css 文件中获得“宽度”。其他一切正常。我从同一页面检查 chrome 浏览器中的 css,它已完全加载。我做错了什么?
加载 css/js 文件的脚本(我在网上找到的):
function loadjscssfile(filename, filetype){
if (filetype=="js"){ //if filename is a external JavaScript file
var fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript")
fileref.setAttribute("src", filename)
}
else if (filetype=="css"){ //if filename is an external CSS file
var fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", filename)
}
if (typeof fileref!="undefined")
document.getElementsByTagName("head")[0].appendChild(fileref)
}
}
和其他jquery代码:
$(this).addClass('iG_Main');
loadjscssfile(data.theme_url,'css');
createGrid($($(this).selector+'.iG_Main'));
function createGrid( selector ){
var cellWidth, columnValue;
var blank = ' ';
selector.append('<div class="iG_Header"></div>');
selector.append('<div class="iG_Container"></div>');
selector.append('<div class="iG_Footer">This is footer!</div>');
selector_header = selector.children('.iG_Header');
selector_container = selector.children('.iG_Container');
selector_footer = selector.children('.iG_Footer');
selector_header.append('<div class="iG_Cell iG_CheckBox inHead"><div id="iG_CheckBox">'+ blank +'</div></div>');
if(undefinedVal(data.width))
data.width = 1000;
if(undefinedVal(data.height))
data.height = 400;
for(var i in data.headerData)
selector_header.append('<div id="'+ data.headerData[i].name +'" class="iG_Cell inHead"><div>'+ data.headerData[i].label +'</div></div>');
if(data.options){
selector_header.append('<div class="iG_Cell Options inHead"><div>'+ blank +'</div></div>');
// cellWidth = (data.width-40-15)/(data.headerData.length);
cellWidth = (data.width-$('.iG_Cell.inHead.Options').width()-selector_header.children('.iG_CheckBox').width())/(data.headerData.length);
}else cellWidth = (data.width-15-selector_header.children('.iG_CheckBox').width())/(data.headerData.length); //15 - in sake of scrollbar
//}else cellWidth = (data.width-15-40)/(data.headerData.length); //15 - in sake of scrollbar
for(var i in data.contentData){
selector_container.append('<div class="iG_Content"></div>');
selector_container.children('.iG_Content:last-child').append('<div class="iG_Cell iG_CheckBox inContent"><div id="iG_CheckBox">'+ blank +'</div></div>');
for(var j in data.headerData){
columnValue = (undefinedVal(data.contentData[i][data.headerData[j].name])) ? blank : data.contentData[i][data.headerData[j].name];
selector_container.children('.iG_Content:last-child').append('<div class="iG_Cell"><div>'+ columnValue +'</div></div>');
}
selector.css({'width':data.width, 'height':data.height});
selector_header.children('.iG_Cell').not('.Options').not('.iG_CheckBox').css('width',cellWidth);
selector_container.children('.iG_Content').children('.iG_Cell').not('.iG_CheckBox').css('width',cellWidth);
selector_container.css('height', data.height-selector_header.height()-selector_footer.height());
}