在一定范围内获取文本

时间:2013-09-02 21:57:16

标签: javascript jquery

我在这个令人满意的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)

1 个答案:

答案 0 :(得分:0)

var text = document.getElementById('board').textContent;

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


如果你想要更多jQuery-ish方式,请使用:

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