簡體   English   中英

使文本在一定范圍內

[英]Getting the text inside a range of span

我的內容可編輯div中有如下文本:

int main(){
    if( (x>1) && (x<10) )
       statement;
}

它的HTML是這樣的:

<div id="board">
   <div>int main<span class="openParen bm1">(</span><span class="closeParen bm1">)</span>{</div>
   <div>if<span class="openParen bm2">(</span> <span class="openParen bm3">(<span>x>1<span class="closeParen bm3">)</span> && <span class="openParen bm4">(</span>x<10<span class="closeParen bm4">)</span> <span class="closeParen bm2">)</span></div>
   <div>statement;</div>
   <div>}</div>
</div>

我在我按行和按列循環的那部分(基於在該行中找到的字符數)

for(loop for each row){
   for(loop for each character of each 

  //on the part where the kword_search has found 'if'row 

  //therefore im on the 2nd row at the 2nd character(disregarding whitespace)  
     if( /\s+if$/.test(kword_search){

      //How will i get the text within bm2, bm3 and bm4?

     }
   }
}

我將如何獲取bm2,bm3和bm4中的文本,以便可以將它們存儲到這樣的數組中

array[0] = $('.bm2').text(); //((x>1) && (x<10))
array[1] = $('.bm3').text(); //(x>1)
array[2] = $('.bm4').text(); //(x<10)
var text = document.getElementById('board').textContent;

這是小提琴: http : //jsfiddle.net/8n5ZR/


如果您想使用更多類似於jQuery的方式,請使用以下方法:

var text = $('#board').text();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM