2016-03-01 58 views
1

当我在js中解析文本并且想要从多行中检索(DNA序列)查询名称并将其放在段落标签之间时,它无法正常工作。用javascript解析文本:神秘生成的段落标签

(的部分)文本文件:

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 

的代码:
(I第一读线到一个数组)

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>"; 
     } 
    } 


结果: Result

+0

尝试改变
标签由
Walfrat

+0

更改和删除
标签没有帮助 –

回答

3

不要使用

innerHTML += 

生成的前手你的整个HTML,然后将其添加到innerHTML的,我的猜测是,当你使用innerHTML,浏览器会自动添加结束标记。

+0

是的,这的确如此,谢谢!有效。 :)另外,有人可以告诉我为什么我不能通过寻找换行符来结束我的循环吗? –

+0

我需要看到你为此尝试的代码。 – Walfrat

+0

... while(lines [i]!=“\ n”){... –

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; 
+0

是的你是对的,谢谢。 (@Walfrat用他的anwser比较快) –