2011-04-06 15 views
0

我有一個標籤象下面這樣:正則表達式?

<p id="questionText"><span class="qn" id="qn"> 1 </span> hello? * </p> 

我想從文本("Hello")。

我已經做了,成功地用下面的代碼,但.textContent沒有在IE

questionText = $('questionText-').textContent; 
questionText = questionText.replace(/\d+/g,""); 
questionText = questionText.replace("*",""); 
return questionText.replace(/^\s*|\s*$/g,'') ; 

工作,我如何更改代碼,使其工作在兩個?

+0

'$('questionText - ')'不是一個有效的選擇器。如果您使用jQuery或$('questionText')'',如果'$'只是別名'getElementById',您可能想使用'$('#questionText')'。 – ThiefMaster 2011-04-06 11:35:57

回答

0

獲取hello? *一部分,而不是整個1 hello? *的就會好很多。要做到這一點,我們需要得到#questionText的第二子節點:

var qt = $("#questionText")[0]; 
qt.normalize(); 
var hello = qt.childNodes[1].data; 
alert(hello); // outputs " hello? * " 

現在我們可以過濾不需要的字符這樣的字符串,

hello = hello. 
      replace(/[^a-zA-Z ]/g, ""). 
      trim(); 
alert(hello); // outputs "hello" 

所有剩下的就是能夠利用第一信。