使用JavaScript生成HTML不起作用? [英] Generate HTML with JavaScript not working?

查看:107
本文介绍了使用JavaScript生成HTML不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JavaScript很陌生,所以我不明白什么是不工作的。
代码:

  var postCount = 0; 

函数generatePost(标题,时间,文本){
var div = document.createElement(div);
div.className =content;
div.id =post_+ postCount;
document.getElementById(postcontainer)。appendChild(div);

var h3 = document.createElement(h3);
div.id =post_h3_+ postCount;
h3.innerHTML = title;
document.getElementById(post_+ postCount).appendChild(div);

var span = document.createElement(span);
document.getElementById(post_h3_+ postCount).appendChild(div);
span.innerHTML = time;

var段落[] = text.split(||);
for(var p:paragraph [] {
var paragraphCount = 0;
var h3 = document.createElement(h3);
document.getElementById(post_p_+ postCount +_+ paragraphCount).appendChild(div);
paragraphCount ++;
}
postCount ++;
}

函数loadPosts(){
generatePost(Testing Title,I do not know,This is || a paragraph);
}

我将它包含在内:

 < body onload =loadPosts() > 

最后,什么也没有显示出来,即使在我的浏览器中的Inspector中也没有。我的代码甚至运行了吗?我忘记了一个基本的doStuffNow()?第二:如果我用JavaScript添加一个类到div,在样式中执行CSS规则.css附加到它吗? 为解决问题的第二部分,适用于类的CSS样式将被添加到o中注意你添加了相同的类。


I'm quite new to JavaScript so I don't understand what's not working. The Code:

var postCount = 0;

function generatePost(title, time, text) {
    var div = document.createElement("div");
    div.className = "content";
    div.id = "post_" + postCount;
    document.getElementById("postcontainer").appendChild(div);

    var h3 = document.createElement("h3");
    div.id = "post_h3_" + postCount;
    h3.innerHTML = title;
    document.getElementById("post_" + postCount).appendChild(div);

    var span = document.createElement("span");
    document.getElementById("post_h3_" + postCount).appendChild(div);
    span.innerHTML = time;

    var paragraphs[] = text.split("||");
    for (var p : paragraphs[] {
        var paragraphCount = 0;
        var h3 = document.createElement("h3");
        document.getElementById("post_p_" + postCount + "_" + paragraphCount).appendChild(div);
        paragraphCount++;
    }
    postCount++;
}

function loadPosts() {
    generatePost("Testing Title", "I don't know", "This is || a paragraph");
}

I included it with:

<body onload="loadPosts()">

In the end, nothing shows up. Not even in the Inspector in my Browser. Is my Code even run? Did I forget an essential doStuffNow()?

Second: If I add a class to a div with JavaScript, do the CSS-Rules in the style.css append to it?

解决方案

To answer the second part of your question, yes, the CSS styling that applies to a class will be added to an object that you add the same class to.

这篇关于使用JavaScript生成HTML不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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