javascript - 动态加载的 css 不设置宽度属性

标签 javascript jquery css

我正在尝试使用 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());
}

最佳答案

由于加载 CSS 的方式与 JS 执行异步,因此可能会发生代码在加载 CSS 之前执行的情况。

为了避免此类问题,我会创建一个内联样式,对 CSS 文件执行 XHR 请求并将内容放入样式元素,例如像那样:

$.get(the_url, function(response) {
  $('head').append('<style id="the_theme"></style>');
  $('#the_theme').text(response);

  // do the rest
});

编辑:当然,这可能会导致相对 URL 出现问题,现在相对于您的文档,而不是您的 CSS 文件。如果这对您来说是个问题,您必须以复杂的方式来解决:在一个时间间隔内检查规则长度:

cssLoaded = 0;
var iv = window.setInterval(function() {
  var cssStylesheet = document.getElementById("the_link_element");
  if(cssStylesheet.sheet && cssStylesheet.sheet.cssRules.length > 0) {
    cssLoaded = 1;
  }
  else if(cssStylesheet.styleSheet && cssStylesheet.styleSheet.cssText.length > 0) {
    cssLoaded = 1;
  }
  else if(cssStylesheet.innerHTML && cssStylesheet.innerHTML.length > 0) {
    cssLoaded = 1;
  }

  if(cssLoaded) {
    window.clearInterval(iv);
  }
}, 500);

关于javascript - 动态加载的 css 不设置宽度属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13525928/

相关文章:

javascript - 具有路由分离的 Node.js 变量作用域

jquery - AJAX 页面加载但 DIV 未扩展以适合其内容

javascript - 更改动态列表项的 CSS

jquery - 将 DOM DIV 附加到 jQuery 动态创建的 DIV

javascript - 如何在 d3.js/JavaScript 中将零值添加到时间序列中

javascript - 谷歌分析 : prevent sending page title

css - -ms-view-state 与标准 CSS 媒体查询表达式有何不同?

javascript - HTML/CSS : 10x10 div boxes with equal margin, 但由于某种原因高度比宽度长

javascript - 在 Aurelia 中,如何根据按键事件获取文本区域内光标的坐标?

javascript - asp.net mvc ajax post传统参数序列化