Javascript 问题编号重新排序
我的javascript文件中有一个问题列表。每个问题都有一个问题编号、问题描述和选项。问题可以添加到问题列表中的任何位置。因此,如果一个问题被添加到所有问题的顶部,那么我需要更改下面所有问题的问题编号。如何才能做到这一点。我可以使用javascript吗?我建议对每个问题使用Javascript 问题编号重新排序,javascript,Javascript,我的javascript文件中有一个问题列表。每个问题都有一个问题编号、问题描述和选项。问题可以添加到问题列表中的任何位置。因此,如果一个问题被添加到所有问题的顶部,那么我需要更改下面所有问题的问题编号。如何才能做到这一点。我可以使用javascript吗?我建议对每个问题使用,并让页面处理数字的回流 否则,您需要在插入之前设置目标编号,对于列表中的每个元素,您将检查其编号是否大于目标编号,如果大于目标编号,则将编号增加1 var Target = //new number that I wan
,并让页面处理数字的回流
否则,您需要在插入之前设置目标编号,对于列表中的每个元素,您将检查其编号是否大于目标编号,如果大于目标编号,则将编号增加1
var Target = //new number that I want the inserted question to be
foreach (element in list) {
if (element.Number > Target) element.Number += 1;
}
list.add( //new question with # set to Target );
您可以使用有序列表(ol)和jQuery执行类似操作:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type ="text/javascript">
$(function(){
var n = 2;
$('button').delegate('click', function(){
$(this).parents('li').after('<li><p><span>Question'+n+'</span><button>Create new question</button></p></li>');
n += 1;
})
})
</script>
</head>
<body>
<ol>
<li>
<p><span>Question 1</span><button>Create new question</button></p>
</li>
</ol>
</body>
</html>
$(函数(){
var n=2;
$('button')。委托('click',函数(){
$(this).parents('li')。在('li>问题'+n+'创建新问题')之后;
n+=1;
})
})
问题1创建新问题
这很有效
var questions=[“一个foo走进一家酒吧,发生了什么事?”,“foo为什么过马路?”;
添加问题(“foo”,1);
功能添加问题(问题、位置)
{
如果(位置>0&&位置<问题长度)
{
var firstHalf=问题。切片(0,位置);
var secondHalf=questions.slice(位置、问题、长度);
上半场。推(问题);
问题=上半部分。concat(下半部分);
控制台日志(“切片”);
}else if(位置=问题长度)
{
问题。推(问题);
控制台日志(“结束”);
}
updateQuestionList();
}
函数updateQuestionList()
{
var questions_list=document.getElementById(“questions_list”);
问题_list.innerHTML=“”;
对于(var i=0;i阵列原型(fun!=):
//基于零的插入
Array.prototype.insert=函数(索引,项){
变量i=0,列表=[];
if(this.length==索引){
列表=此;
列表。推送(项目);
退货清单;
}
对于(;i
谢谢。但我不明白您所说的页面句柄回流的意思numbers@Rohan你有没有见过一个正在运行的?请访问此页面并在左侧添加一些元素。@Toader…我忘记缩进了吗/facepalm@Toader我们每个人都会在某个时候遇到……啊,好吧~谢谢你的帮助;)
var questions = ["A foo walks into a bar. What happens?", "Why did foo cross the road?"];
addQuestion("foo", 1);
function addQuestion(question, position)
{
if(position > 0 && position < questions.length)
{
var firstHalf = questions.slice(0, position);
var secondHalf = questions.slice(position, questions.length);
firstHalf.push(question);
questions = firstHalf.concat(secondHalf);
console.log("slice");
}else if(position <= 0)
{
questions.unshift(question);
console.log("beginning");
}else if(position >= questions.length)
{
questions.push(question);
console.log("end");
}
updateQuestionList();
}
function updateQuestionList()
{
var questions_list = document.getElementById("questions_list");
questions_list.innerHTML = "";
for(var i=0;i<questions.length;i++)
{
var question = document.createElement("LI");
question.innerHTML = questions[i];
questions_list.appendChild(question);
}
}
// zero-based insert
Array.prototype.insert = function(index, item) {
var i = 0, list = [];
if (this.length == index) {
list = this;
list.push(item);
return list;
}
for(; i < this.length; i++) {
if (index == list.length) {
list.push(item);
i--;
} else {
list.push(this[i]);
}
}
return list;
};
Array.prototype.print = function (base) {
base = base || 1;
for (var i = 0, out = []; i < this.length; i++) {
out.push((base + i) + '. ' + this[i]);
}
return out.join("\n");
};
list = ['when?', 'where?', 'why?'];
list = list.insert(0, 'who?'); // first: ["who?", "when?", "where?", "why?"]
list = list.insert(3, 'how?'); // 4th: ["who?", "when?", "where?", "how?", "why?"]
list = list.insert(list.length, 'last?'); // last: ["who?", "when?", "where?", "how?", "why?", "last?"];
list.print();
/**
"1. who?
2. when?
3. where?
4. how?
5. why?
6. last?"
**/