css not being applied to document.write text

半城伤御伤魂 提交于 2019-11-28 13:45:53
Zoltan Lengyel

Use innerHTML.

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

then you can write in it like this:

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

If you run your document.write() before the page finishes loading (perhaps calling your showFolder call directly from a script on the page), then the text will be written into the document as you might expect.

However, if you call document.write after the page loads, as in an event handler, you will be writing an entirely new page. This is usually not what you want.

Instead, follow Zoltan's advice and set the innerHTML property of an empty div.

I'm not javascript expert... I mainly use jQuery.. but try this, kind of makes sense:

<!DOCTYPE html>

TITLE GOES HERE

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

<link rel="stylesheet" href="css/popup.css" type="text/css" />

EDIT:

So the above didn't work, but I just thought about another solution. When are you actually calling the function? Try to put it in <body onLoad="functionnamehere()">

No idea if that works, but give it a try.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!