2016-03-01 67 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比較快) –