2012-01-18 2 views
2

다음 코드 단편은 HTML 코드로 새 문서 개체를 만듭니다. 다음 문을 참조하여 첨부 된 스타일 시트 배열을 가져와야하지만 ZERO 결과가 나타납니다.createHTMLDocument 메서드를 통해 만든 문서에서 styleSheets를 추출하는 방법

doc.styleSheets 

그러나 유사한 문 제가 잘못하고있어 무엇을 찾을 수

document.styleSheets. 

도움말처럼 현재 문서에 대해 잘 작동합니다.

var doc = document.implementation.createHTMLDocument(title); 
doc.documentElement.innerHTML = ' 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <title>Title</title> 
    <link rel="stylesheet" href="http://localhost/style.css" type="text/css" /> 
    </head> 
    <body> 
     Hello world! 
    </body> 
</html>'; 

var sheets = doc.styleSheets; // it gives ZERO results 
+0

새 문서와 관련된 자원은 DOM에 추가 될 때까지로드되지 않습니다. 이것을 달성하기 위해 iframe 메서드를 사용하는 것을 제외하고는 어떤 방식입니까? –

답변

0

흠 ...

당신은 혹시 jQuery를 (또는 이와 유사한 프레임 워크)를 사용하고 있습니까? 그렇다면 DOMREADY 이벤트가 발생할 때까지 기다렸다가 스타일 시트 배열을 가져올 수 있습니다.

자세한 내용은 @post을 참조하십시오.

관련 문제