繁体   English   中英

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

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

因此,我尝试显示Firebase数据库中的所有问题和答案。 它显示的很好,但看起来很丑,因为问题和答案之间没有空格。 我尝试使用createElement功能以及.innerHTML添加不间断的空间。 什么都没用。 这是到目前为止的代码:感谢您的帮助!

<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>

您可以通过添加<hr>元素来添加一行,如下所示: http : //www.htmlgoodies.com/tutorials/getting_started/article.php/3479441

像这个:


您还可以为每个元素添加<div>部分,并使用CSS设置边距,内边距和边框的样式。 <p>部分相同。

您可以使用此JSFiddle:

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

还可以通过CSS设置边框样式,也可以查看以下内容: http : //www.w3schools.com/css/css_border.asp

暂无
暂无

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

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