Javascript div中的数组元素配对

Javascript div中的数组元素配对,javascript,html,css,arrays,Javascript,Html,Css,Arrays,我有一个脚本,可以从文本文件中读取并插入元素,以便对它们进行样式设置和显示。但是,我现在想在div中配对两个元素。代码如下: var lines = request.responseText.replace(/\r/g, "").split("\n"); // Remove carriage returns (\r) and place text into an array, split at the newline character (\n) for (var i =

我有一个脚本,可以从文本文件中读取并插入元素,以便对它们进行样式设置和显示。但是,我现在想在div中配对两个元素。代码如下:

var lines = request.responseText.replace(/\r/g, "").split("\n");    // Remove carriage returns (\r) and place text into an array, split at the newline character (\n)
        for (var i = 0; i < lines.length; i++) {    // Cycle through each line in the array
            if (lines[i].length >= 57) {    // Check if the current line is lengthy, and if so.....separate function
            }
            var coup = document.createElement("div");    // Create a div element
            coup.id = "line" + i;    // Give the div an ID in the form of line0, line1, etc.
            coup.innerText = coup.textContent = lines[i];    // Place the current line of text in the div
            el_status.appendChild(coup);    // Append the div to the status box
        }
var lines=request.responseText.replace(/\r/g,“”).split(“\n”);//删除回车符(\r)并将文本放入数组中,在换行符处拆分(\n)
对于(var i=0;i=57){//检查当前行是否过长,如果过长….则分离函数
}
var coup=document.createElement(“div”);//创建一个div元素
coup.id=“line”+i;//以第0行、第1行等形式为div指定一个id。
coup.innerText=coup.textContent=lines[i];//将当前文本行放在div中
el_status.appendChild(coup);//将div附加到状态框中
}
我希望第0行和第1行进入一个分区。然后我希望第2行和第3行进入另一个分区

var coup不必是div,我不介意将其更改为p、span或li

谢谢

var lines=request.responseText.replace(/\r/g,“”).split(“\n”);
var lines = request.responseText.replace(/\r/g, "").split("\n");
for (var i = 1; i < lines.length; i += 2) {
    var coup = document.createElement("div");
    coup.id = "line" + i;
    coup.textContent = lines[i - 1] + lines[i];

    el_status.appendChild(coup);
}​
对于(变量i=1;i

每次只需迭代两次,并将它们放在同一个DIV中,或者放在该DIV的一些变体中,具体取决于您所使用的结构?

尝试
document.createTextNode()的基本方法并在迭代的每一步添加两行

var lines = request.responseText.replace(/\r/g, "").split("\n");    // Remove carriage returns (\r) and place text into an array, split at the newline character (\n)
        for (var i = 0, l = lines.length; i < l; i += 2) {    // Cycle through each line in the array
            if (lines[i].length >= 57) {    // Check if the current line is lengthy, and if so.....separate function
            }
            var coup = document.createTextNode(lines[i-1] + lines[i]);    // Create a div element
            coup.id = "line" + i;    // Give the div an ID in the form of line0, line1, etc.
            coup.innerText = coup.textContent = lines[i];    // Place the current line of text in the div
            el_status.appendChild(coup);    // Append the div to the status box
        }
var lines=request.responseText.replace(/\r/g,“”).split(“\n”);//删除回车符(\r)并将文本放入数组中,在换行符处拆分(\n)
对于(var i=0,l=lines.length;i=57){//检查当前行是否过长,如果过长….则分离函数
}
var coup=document.createTextNode(行[i-1]+行[i]);//创建一个div元素
coup.id=“line”+i;//以第0行、第1行等形式为div指定一个id。
coup.innerText=coup.textContent=lines[i];//将当前文本行放在div中
el_status.appendChild(coup);//将div附加到状态框中
}
此外,DOM操作相当昂贵,在for循环中执行附加操作可能会减慢速度。所以我宁愿这样做:

var lines = request.responseText.replace(/\r/g, "").split("\n");    // Remove carriage returns (\r) and place text into an array, split at the newline character (\n)
var appendedLines = [];//create a new array.
        for (var i = 0, l = lines.length; i < l; i += 2) {    // Cycle through each line in the array
            if (lines[i].length >= 57) {    // Check if the current line is lengthy, and if so.....separate function
            }
            var coup = document.createTextNode(lines[i-1],lines[i]);    // Create a div element
            coup.id = "line" + i;    // Give the div an ID in the form of line0
            appendedLines.push(coup);   // Append the div to the status box
        }
el_status.appendChild(appendedLines.join(''));// this uses a single append statement.
var lines=request.responseText.replace(/\r/g,“”).split(“\n”);//删除回车符(\r)并将文本放入数组中,在换行符处拆分(\n)
var appendedLines=[]//创建一个新数组。
对于(var i=0,l=lines.length;i=57){//检查当前行是否过长,如果过长….则分离函数
}
var coup=document.createTextNode(行[i-1],行[i]);//创建一个div元素
coup.id=“line”+i;//以line0的形式为div指定一个id
appendedLines.push(coup);//将div追加到状态框中
}
el_status.appendChild(appendedLines.join(“”));//这使用一个append语句。

另外,
l=lines.length
的目的是进一步加快速度。当使用带有
i
条件的for循环时,JS解释器将在迭代的每个步骤中查找
someArray.length
。存储对
someArray.length的引用修复了这一问题

顺便说一句,像
//创建div元素
这样的明显评论会适得其反。感谢您的反馈。我试过你的建议,但效果不好。文本文件包含9行,但并非所有行都将配对。感谢您的输入。我试过你的建议,但效果不好。只有if语句起作用,但没有生成行。