2011-05-06 37 views
0

我有以下塊獲取文本值,正則表達式或DOM方法?

<span id="title"><select id="optionType"><option>choose option</option><option> hello</option><option>how are you</option></select></span> 

我想刪除第一個選項,需要得到在新線所有其他選項中,只有選項的文本與任何標記。正則表達式是最好的使用方法,還是用其他更好的方法來檢索新行中的文本?我嘗試以下,但它可能會失敗,如果有可能不是正則表達式somtimes

Description= $('title').innerHTML; 
var tmp = Description; 

tmp = tmp.replace("--Choose an option--</option>",""); // remove first option 
tmp = tmp.replace(/<\/option><option>/g, "<\/option>\n<\/option>"); 
tmp = tmp.replace(/(<([^>]+)>)/g,""); 
tmp = tmp.replace(/\n/g, "\n"); 
tmp = tmp.replace(/^[\s]*/gm, ''); 

return tmp; 
+1

因爲我們是在Javascript(因此在瀏覽器中可能),這將是更容易使用標準DOM方法而不是使用Regex。 – Spudley 2011-05-06 12:03:19

+1

嘗試使用更具描述性的標題並正確設置您的文章的格式。這會提高回答率和質量。 – 2011-05-06 12:05:23

+0

@Spudley:你能從DOM方法開始幫助我嗎?它在IE中工作嗎? – kiran 2011-05-06 12:06:26

回答

0
(<option[^>]*?>([^<]+)<\/option>) 
1

jQuery的嘗試。

它會像

$("#optionType>option:first").remove() //to delete first option 
$("#optionType>option") //list of other options 
0

你有沒有考慮只刪除選項,而不是直接用HTML代碼擺弄?

特別容易,因爲你使用JQuery已經:

$("#title option[value='--Choose an option--']").remove(); 

參見:Removing an item from a select box