简体   繁体   中英

Dynamic HTML page not using css

I'm trying to dynamically set the content of a popup.

Here is a first HTML page where everything is defined statically :

<html>
    <head>
        <meta charset='utf-8'>
        <link href='css/font-awesome.css' rel='stylesheet'>
        <link href='css/myStyle.css' rel='stylesheet'>
    </head>

    <body>
        <div id="data">
            <ul class='links-list'>
                <li><a target='_blank' href='siteURL'><i class='myButton'>TEXT</i></a></li>
                <li><a target='_blank' href='twitterURL'><i class='myButton'>TEXT</i></a></li>
            </ul>
        </div>
    </body>
</html>

Now I need to dynamically set my buttons, so I've removed everything which will be dynamically created :

<html>
    <head>
        <meta charset='utf-8'>
        <link href='css/font-awesome.css' rel='stylesheet'>
        <link href='css/myStyle.css' rel='stylesheet'>
    </head>

    <body>
        <div id="data">
        </div>

        <script type="text/javascript" src="script.js">
        </script>
    </body>
</html>

My content script "script.js" receive data (array of links) and have to create buttons in my HTML document :

self.port.on("liste", function(list)
{
    var div = document.getElementById('data'); // Get <div id="data">
    var ul = document.createElement('ul'); // Create <ul class='links-list'>
    ul.class = 'links-list';

    for (var i = 0; i < list.links.length; ++i)
    {
        var site = list.links[i];
        var li = document.createElement('li'); // Create <li>
        var link = document.createElement('a'); // Create <a>
        var button = document.createElement('i'); // Create <i>

        button.class = "myButton";

        link.text = site.text;
        link.href = site.url;
        link.target = '_blank';

        link.appendChild(button);
        li.appendChild(link);
        ul.appendChild(li);
    }

    div.appendChild(ul);
});

Issue is links created dynamically aren't using "myStyle.css", here is a comparaison :

Static vs dynamic load :

静态与动态负载

Could anyone help me resolving this? Thank you.

The correct way to give an item a class using javascript is - unintuitively enough - className , or setAttribute . So either of these will add the correct class:

button.className = 'myButton'
button.setAttribute('class', 'myButton')

Using just .class does not work in Javascript:

 document.getElementById('a1').class = 'aClass'; document.getElementById('a2').className = 'aClass'; document.getElementById('a3').setAttribute('class', 'aClass'); 
 .aClass { color: red; } 
 <pre id="a1">.class</pre> <pre id="a2">.className</pre> <pre id="a3">.setAttribute</pre> 

Looks to me like the comment from CBroe is the answer. In your javascript you're putting the text into the link instead of your button. That means that the button will essentially be invisible. That's why it looks different from your hard-coded example. Try this javascript instead.

var div = document.getElementById('data'); // Get <div id="data">
var ul = document.createElement('ul'); // Create <ul class='links-list'>
ul.className = 'links-list';

for (var i = 0; i < 4; ++i){
    var url = i;
    var li = document.createElement('li'); // Create <li>
    var link = document.createElement('a'); // Create <a>
    var button = document.createElement('i'); // Create <i>

    button.className = "myButton";
    button.innerHTML = 'text'+i;

    link.text = "ab ";
    link.href = url;
    link.target = '_blank';

    link.appendChild(button);
    li.appendChild(link);
    ul.appendChild(li);
}

div.appendChild(ul);

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