简体   繁体   中英

How to extract styleSheets from the document created via createHTMLDocument method

The following code snippet will create new document object with the HTML code. By referencing the following statement, I should get the array of attached stylesheets, but I'm getting ZERO results.

doc.styleSheets

But the similar statement works fine for the current document like

document.styleSheets.

Help me to find what's I'm doing wrong.

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

Hmmm...

Are you using jQuery (or some similar framework) by any chance? If so, you could try waiting until the DOMREADY event fires, then grab your array of stylesheets.

Take a look @ this post for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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