简体   繁体   English

使用javascript在元素之间添加空格/换行

[英]Add space/new line between elements with javascript

So I am attempting to display all the questions and responses from my Firebase database. 因此,我尝试显示Firebase数据库中的所有问题和答案。 It is showing up fine, but it looks ugly, because there is no space between the question and responses. 它显示的很好,但看起来很丑,因为问题和答案之间没有空格。 I've tried using the createElement feature as well as .innerHTML to add a nonbreaking space. 我尝试使用createElement功能以及.innerHTML添加不间断的空间。 Nothing is working. 什么都没用。 Here is the code I have thus far: Thanks for your help! 这是到目前为止的代码:感谢您的帮助!

<button id="all" onclick="button()"> View All </button>
<h4> All Users: </h4>

<script>
    function button(){
        var userRef = new Firebase("https://speedpoll-1fd08.firebaseio.com");
        userRef.on("value", function(snapshot) {
            // The callback function will get called twice, once for "fred" and once for "barney"
            snapshot.forEach(function(childSnapshot) {
                // key will be "fred" the first time and "barney" the second time
                var key = console.log(childSnapshot.key());
                // childData will be the actual contents of the child
                // var userInfo = console.log(childSnapshot.val());
                var element = document.getElementById("viewAll");
                var para = document.createElement("h5");

                var node = document.createTextNode("Question: " + childSnapshot.key());
                console.log(childSnapshot.child("Option1").child('Response1').val());
                var node1= document.createTextNode("Response 1: " + childSnapshot.child("Option1").child('Response1').val());
                //var space = document.createElement("&nbsp;");
                element.innerHTML += "&nbsp;";
                var node2= document.createTextNode("Response 2: " + childSnapshot.child('Option2').child('Response2').val());
                var node3= document.createTextNode("Response 3: " + childSnapshot.child('Option3').child('Response3').val());
                para.appendChild(node);
                //para.appendChild(space);
                para.appendChild(node1);
                para.appendChild(node2);
                para.appendChild(node3);
                element.appendChild(para);

            });
        });

    }
</script>
<div id="viewAll">
</div>

You can add a line by adding an <hr> element, as explained here: http://www.htmlgoodies.com/tutorials/getting_started/article.php/3479441 您可以通过添加<hr>元素来添加一行,如下所示: http : //www.htmlgoodies.com/tutorials/getting_started/article.php/3479441

Like this one: 像这个:


You can also add <div> sections for each element, and style the margins, paddings and borders with CSS. 您还可以为每个元素添加<div>部分,并使用CSS设置边距,内边距和边框的样式。 The same for <p> sections. <p>部分相同。

You can play around with this JSFiddle: 您可以使用此JSFiddle:

  http://jsfiddle.net/jmgomez/n0e1ev8e/ 

Check this out also on how to style borders with CSS: http://www.w3schools.com/css/css_border.asp 还可以通过CSS设置边框样式,也可以查看以下内容: http : //www.w3schools.com/css/css_border.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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