css不适用于document.write文本 [英] css not being applied to document.write text

查看:254
本文介绍了css不适用于document.write文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用document.write为Chrome扩展程序向页面写入文本,但不应用关联的自定义CSS:

I'm writing text to a page using document.write for a Chrome extension, but the associated custom CSS isn't applied:

<!DOCTYPE html>
<html>
<head>
    <title>TITLE GOES HERE</title>
     <link rel="stylesheet" href="css/popup.css" type="text/css" />
</head>

<body>
     <script type="text/javascript">
        ...
        function showFolder(folder) {
            console.debug('FOLDER: '+folder.title);
            document.write('<p>'+folder.title+'<br></p>');
        }
    </script>

</body>
</html>

CSS很简单,只是为了调试:

The CSS is simple, just for debugging:

p {
color: red;
}



我可以让它工作,如果我把样式表链接里面的函数showFolder ,但这不能是正确的方法。我正在学习jscript / CSS,所以答案可能是补救。是问题在jscript,CSS还是两者?

I can get it to work if I put the stylesheet link inside the function showFolder, but that can't be the proper way to do it. I'm learning jscript/CSS on the fly, so the answer is probably something remedial. Is the problem in the jscript, the CSS or both?

推荐答案

使用innerHTML。

Use innerHTML.

<div id="towrite"></div>

那么你可以这样写:

div=document.getElementById('towrite');
div.innerHTML = '<p>'+folder.title+'<br></p>';

这篇关于css不适用于document.write文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆