当我在js中解析文本并想要从多行检索(DNA序列)查询名称并将其放在段落标记之间时,它将无法正常工作。
(部分)文本文件:
Database: db
22,774 sequences; 12,448,185 total letters
Searching..................................................done
Query= gi|998623327|dbj|LC126440.1| Rhodosporidium sp. 14Y315 genes
for ITS1, 5.8S rRNA, ITS2, partial and complete sequence
(591 letters)
Score E
Sequences producing significant alignments: (bits) Value
代码:
(我先把这些行读成数组)
for(var i = 0; i < lines.length; i++){
var line = lines[i];
if(line.search("Query= ") != -1){
results.innerHTML += " <p class='result_name'> <br>Result name: ";
//the name starts at 7th char
results.innerHTML += line.slice(7);
//take the next line
i++;
// tried to searh for "\n" or "\r" or "\r\n" to end cycle - didn't work
// so instead I put this for the while condition:
while(lines[i].length > 2 ){
results.innerHTML += lines[i];
i++;
}
//here is where I want the result_name paragraph to end.
results.innerHTML += " </p> <p>Result(s):</p>";
}
}
答案 0 :(得分:3)
不要使用
innerHTML +=
事先生成整个HTML,然后将其添加到innerHTML中,我的猜测是当你使用innerHTML时,浏览器会自动添加结束标记。
答案 1 :(得分:1)
使用部分html填充innerHTML将使用结束标记自动更正。因此,创建一个临时变量来收集您的字符串并立即将其填充到目标中,如下所示。这解决了问题
var temp = "";
for(var i = 0; i < lines.length; i++){
var line = lines[i];
if(line.search("Query= ") != -1){
temp += " <p class='result_name'> <br>Result name: ";
//the name starts at 7th char
temp += line.slice(7);
//take the next line
i++;
// tried to searh for "\n" or "\r" or "\r\n" to end cycle - didn't work
// so instead I put this for the while condition:
while(lines[i].length > 2 ){
temp += lines[i];
i++;
}
//here is where I want the result_name paragraph to end.
temp += " </p> <p>Result(s):</p>";
}
}
results.innerHTML = temp;