简体   繁体   English

HTML内容中的CSS链接在加载HTML时抛出错误

[英]css linking in html content throws get error on loading html

I have a problem with jQuery's .html() method. 我对jQuery的.html()方法有.html() I load a html content into a div by using this: 我使用以下方法将html内容加载到div中:

$('#content').html(domContent);

where domContent is containing a entire HTML string. domContent包含整个HTML字符串。 It has a stylesheet linked to it. 它具有链接到的样式表。 So while this line executes am getting GET file:///.... error in the console, because the URL path of CSS file is not valid. 因此,在执行此行时,控制台中出现GET file:///....错误,因为CSS文件的URL路径无效。

However later I modify the URL and get it to apply it on my page. 但是,稍后我修改了URL,并将其应用到我的页面上。

But I don't want this error to occur in the first place. 但我不希望首先发生此错误。 It should ignore it or if any way I can tell jQuery to skip this GET error. 它应该忽略它,或者是否可以通过任何方式告诉jQuery跳过此GET错误。

You can use $(document.createDocumentFragment()); 您可以使用$(document.createDocumentFragment()); in this instance. 在这种情况下。 As it suggests, it creates a document fragment, but doesn't add it into the page, so won't try to fetch the CSS from the broken URL. 就像它建议的那样,它创建了一个文档片段,但是没有将其添加到页面中,因此不要尝试从断开的URL中获取CSS。 For example: 例如:

var $fragment = $(document.createDocumentFragment());
$fragment.html(domContent);

// Fix the CSS URL using the code you've already got, then:

$('#content').html($fragment.html());

If your unsure how the 'fix CSS URL' code you've got will work with this, just post the code and I'll help out. 如果您不确定所拥有的“修复CSS URL”代码将如何使用此功能,请发布代码,我会帮忙的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM