[英]Javascript ordered list and content
So my problem is about creating new element in ordered list with exact content. 所以我的问题是关于在具有精确内容的有序列表中创建新元素。 I have this homework and I am studying JS only for a couple of days.
我有这份作业,而且我只学习JS几天。 I have to make an ordered list and a button ADD ITEM.
我必须制作一个有序列表和一个按钮“添加项目”。 When I click the button it must create a new element in the ordered list with next number containig in it.
当我单击按钮时,它必须在有序列表中创建一个新元素,并在其中包含下一个数字containig。 So now when I click the button the prompt window appear and the text that I write goes in the next "li", but I can't figure how next "li" will display item 1, item 2, item 3 etc. And also I need to hide the first element cause "item 1: Text always shows up."
因此,现在当我单击按钮时,将出现提示窗口,并且我写的文本将出现在下一个“ li”中,但是我无法确定下一个“ li”将如何显示项目1,项目2,项目3等。我需要隐藏第一个元素,原因是“项目1:始终显示文本”。
function addItem() { var item = prompt("Please insert a text", "It will be added to the list"); if (item != null) { var n = 0; var ol = document.getElementById("myList"); var li = document.createElement("li"); ol.appendChild(li); li.innerHTML += "item " + n + ':' + item; } else { document.getElementById("text").innerHTML = " "; } }
#nested { width: 95%; height: 100%; border: 1px solid red; margin-left: auto; margin-right: auto; } #large { width: 80%; height: 100%; border: 1px solid blue; margin-left: auto; margin-right: auto; } ol { list-style-type: arabic; list-style-position: inside; margin-top: 10px; margin-right: 30px; } .right { float: right; }
<div id="large"> <div id="nested"> <form method="get"> <input type="button" id="resizeBut" name="resize" value="Collapse" onclick="change()" /> <br /> <br /> <input type="button" id="newItem" name="items" value="Add Item" onclick="addItem()" /> </form> <ol reversed="reversed" class="right" id="myList"> Unordered list <li>item 1:<span id="text">Text</span> </li> </ol> </div> </div>
n
outside of the function
function
之外的Decalre var n
Increment
it in every function call Increment
var n = 1; function addItem() { var item = prompt("Please insert a text", "It will be added to the list"); if (item != null) { ++n; var ol = document.getElementById("myList"); var li = document.createElement("li"); li.innerHTML = "item " + n + ':' + item; ol.appendChild(li); } else { document.getElementById("text").innerHTML = " "; } }
#nested { width: 95%; height: 100%; border: 1px solid red; margin-left: auto; margin-right: auto; } #large { width: 80%; height: 100%; border: 1px solid blue; margin-left: auto; margin-right: auto; } ol { list-style-type: arabic; list-style-position: inside; margin-top: 10px; margin-right: 30px; } .right { float: right; }
<div id="large"> <div id="nested"> <form method="get"> <input type="button" id="resizeBut" name="resize" value="Collapse" onclick="change()" /> <br /> <br /> <input type="button" id="newItem" name="items" value="Add Item" onclick="addItem()" /> </form> <ol reversed="reversed" class="right" id="myList"> Unordered list <li>item 1:<span id="text">Text</span> </li> </ol> </div> </div>
The answer depends on if you are able to use libraries or not. 答案取决于您是否能够使用库。 If you can use jQuery this is a lot easier, if not, it is still doable, just more code.
如果您可以使用jQuery,这会容易得多,如果不能,它仍然可行,只需编写更多代码即可。 I will write out an answer with both...
我会同时写一个答案...
jQuery Answer... jQuery答案...
function addItem() {
var item = prompt("Please insert a text", "It will be added to the list");
if (item != null) {
var count = $('#myList').find('ol').find('li').length;
if(count > 1) {
$('#myList').find('ol').find('li').hide();
}
var nextItem = count + 1;
$('#myList').find('ol').append('<li>Item ' + nexItem + ':' + item + '</li>');
} else {
$('#text').html(' ');
}
}
Non jQuery (Vanilla JavaScript)... 非jQuery(Vanilla JavaScript)...
function addItem() {
var item = prompt("Please insert a text", "It will be added to the list");
if (item != null) {
var n = document.getElementById("myList").getElementsByTagName("li").length;
if(n > 0) {
var elements = document.getElementById("myList").getElementsByTagName("li");
for(var i = 0; i < n; i++) {
elements[i].style.visibility = "hidden";
elements[i].style.display = "none";
}
}
var nextItem = n + 1;
var ol = document.getElementById("myList");
var li = document.createElement("li");
ol.appendChild(li);
li.innerHTML += "item " + nextItem + ':' + item;
} else {
document.getElementById("text").innerHTML = " ";
}
}
There is also a way you can do it to speed it up a little by storing the count as a variable and then increment it each time to get the new number, that way you aren't counting DOM elements each time. 还有一种方法可以通过将计数存储为变量然后每次将其递增以获得新数字来稍微加快速度,从而不必每次都计数DOM元素。 It would be a simple addition.
这将是一个简单的补充。 Here is an example of that...
这是一个例子...
//inside a <script > tag
var count = 0;
function addItem() {
var item = prompt("Please insert text here", "It will be added to the list");
if(item != null) {
if(count > 0) {
var items = document.getElementById("myList").getElementsByTagName("li");
for(var i = 0; i < items; i++) {
items[i].style.visibility = "hidden";
items[i].style.display = "none";
}
}
count += 1;
var list = document.getElementById("myList");
var li = document.createElement("li");
li.innerHTML = "item " + count + ": " + item;
list.getElementsByTagName("ol").append(li);
} else {
document.getelementById("text").innerHTML = " ";
}
}
I hope this helps. 我希望这有帮助。 There are many...many ways to solve this, these are only a couple.
有很多方法可以解决此问题,但这只是一对。 Let me know if this isn't working or you need more help.
让我知道这是否无效或您需要更多帮助。
You could also always count how many li
are in your list and use that for n
. 您还可以始终计算列表中有多少
li
并将其用作n
。 For deletion, you just need to hide or remove the item on the first pass. 对于删除,您只需要在第一遍中隐藏或删除该项目。
var ADDED_ITEMS = 0; function addItem() { var placeholder = "It will be added to the list"; var item = prompt("Please insert a text", placeholder); if (item != null) { var ol = document.getElementById("myList"); // Remove the existing LI on the first pass if (++ADDED_ITEMS == 1) ol.removeChild( ol.querySelector('li') ); // Get count of LI in list var n = ol.querySelectorAll('li').length + 1; // Create new LI var li = document.createElement("li"); ol.appendChild(li); li.innerHTML += "item " + n + ':' + item; } else { document.getElementById("text").innerHTML = " "; } }
#nested { width: 95%; height: 100%; border: 1px solid red; margin-left: auto; margin-right: auto; } #large { width: 80%; height: 100%; border: 1px solid blue; margin-left: auto; margin-right: auto; } ol { list-style-type: arabic; list-style-position: inside; margin-top: 10px; margin-right: 30px; } .right { float: right; }
<div id="large"> <div id="nested"> <form method="get"> <input type="button" id="resizeBut" name="resize" value="Collapse" onclick="change()" /> <br /> <br /> <input type="button" id="newItem" name="items" value="Add Item" onclick="addItem()" /> </form> <ol reversed="reversed" class="right" id="myList"> Unordered list <li>item 1:<span id="text">Text</span></li> </ol> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.