开发者

getting text value , regex or DOM method?

I have the following block

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

I want to remove the first option and need to get the all other options in new line, only the option text with out any tags . Is regular expression is the best one to use, or is thire any other better method to retrieve text in new line ? I tried the following but it can fail somtimes

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

tmp = tmp.re开发者_JAVA百科place("--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;


Try with jQuery if possible instead of regex.

It would be something like

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


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


Have you considered just removing the option directly rather than fiddling with the HTML code?

Particularly easy, since you're using JQuery already:

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

See also: Removing an item from a select box

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜