[英]Create ul and li elements in javascript.
I am trying to create ul and li element in my codes by using javascript. 我试图通过使用JavaScript在我的代码中创建ul和li元素。 I have following: 我有以下内容:
var test=document.createElement('section');
test.setAttribute('id','test');
var ul=document.createElement('ul');
document.body.appendChild(test);
test.appendChild(ul);
for (var i=0; i<array.length; i++){
var li=document.createElement('li');
ul.appendChild(li);
li.innerHTML=li.innerHTML + array[i];
}
My array is parsed using json_encode from php 我的数组是使用php中的json_encode解析的
array[0]='aaa';
array[1]='bbb';
array[2]='ccc';
CSS CSS
section {
display: block;
width: 200px;
margin: 50px auto;
border: 3px solid red;
}
Everything works fine except the list style dot is outside of my section tag. 一切正常,除了列表样式点在我的section标签之外。
It displays like this in Chrome and IE but firefox work fine. 它在Chrome和IE中显示如下,但firefox工作正常。
-------------
*| aaa |
*| bbb |
*| ccc |
-------------
I want my dot inside my section tag. 我希望我的部分标记内的点。 Anyone idea? 谁有想法? Thanks a lot. 非常感谢。
Here is my working code : 这是我的工作代码:
<!DOCTYPE html>
<html>
<head>
<style>
ul#proList{list-style-position: inside}
li.item{list-style:none; padding:5px;}
</style>
</head>
<body>
<div id="renderList"></div>
</body>
<script>
(function(){
var ul = document.createElement('ul');
ul.setAttribute('id','proList');
productList = ['Electronics Watch','House wear Items','Kids wear','Women Fashion'];
document.getElementById('renderList').appendChild(ul);
productList.forEach(renderProductList);
function renderProductList(element, index, arr) {
var li = document.createElement('li');
li.setAttribute('class','item');
ul.appendChild(li);
li.innerHTML=li.innerHTML + element;
}
})();
</script>
</html>
使用CSS
属性list-style-position
来定位项目符号:
list-style-position:inside /* or outside */;
Great then. 那很好。 Let's create a simple function that takes an array and prints our an ordered listview/list inside a div tag. 让我们创建一个简单的函数,它接受一个数组并在div标签内打印一个有序的列表视图/列表。
Step 1: Let's say you have an div with "contentSectionID" id. 第1步:假设你有一个带有“contentSectionID”id的div。 <div id="contentSectionID"></div>
Step 2: We then create our javascript function that returns a list component and takes in an array: 第2步:然后我们创建我们的javascript函数,它返回一个列表组件并接受一个数组:
function createList(spacecrafts){
var listView=document.createElement('ol');
for(var i=0;i<spacecrafts.length;i++)
{
var listViewItem=document.createElement('li');
listViewItem.appendChild(document.createTextNode(spacecrafts[i]));
listView.appendChild(listViewItem);
}
return listView;
}
Step 3: Finally we select our div and create a listview in it: 第3步:最后我们选择我们的div并在其中创建一个列表视图:
document.getElementById("contentSectionID").appendChild(createList(myArr));
Try out below code snippet: 试试下面的代码片段:
var list = [];
var text;
function update() {
console.log(list);
for (var i = 0; i < list.length; i++) {
console.log( i );
var letters;
var ul = document.getElementById("list");
var li = document.createElement("li");
li.appendChild(document.createTextNode(list[i]));
ul.appendChild(li);
letters += "<li>" + list[i] + "</li>";
}
document.getElementById("list").innerHTML = letters;
}
function myFunction() {
text = prompt("enter TODO");
list.push(text);
update();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.