简体   繁体   中英

How to remove unwanted spaces in html tags using Javascript?

I have one Javascript variable. The variable is given below

 var str = '< table class = "buy_selection" >
 < tbody > 
 < tr >
 < th > size < / th >
 < td >
 < select id = "Specification_select" name = "Specification" >
 < option value = "014 black / white" > 014 black / white < / option >
 < option value = "100 white/v red" > 100/v red white < / option > 
 < option value = "101 white/black" > 101 white/black < / option > 
 < option value = "102 white/white" > 102/white < / option >
 < / select > 
 < / Td >
 < / tr > 
 < tr >
 < th > color < / th > 
 < td >
 < select id = "Specification_select" name = "Specification" >
 < option value = "23.0" > 23.0 < / option > 
 < option value = "23.5" > 23.5 < / option >
 < option value = "24.0" > 24.0 < / option > 24.5 
 < option value = "24.5" > < / option > 
 < option value = "25.0" > 25.0 < / option >
 < option value = "25.5" > 25.5 < / option >
 < option value = "26.0" > 26.0 < / option > 
 < option value = "26.5" > 26. 5 < / option > 
 < option value = "27.0" > 27.0 < / option > 
 < option value = "27.5" > 27.5 < / option > 
 < option value = "28.0" > 28.0 < / option >
 < option value = "28.5" > 28.5 < / option > 
 < option value = "29.0" > 29.0 < / option > 
 < option value = "30.0" > 30.0 < / option >
 < option value = "30.5" > 30.5 < / option > 
 < option value = "31.0" > 31.0 < / option > 
 < / select > 
 < / td >
 < / tr > write < tr > 
 < th > reviews 10 0 Yen discount < / th >
 < td >
 < select id = "Specification_select" name = "Specification" >,
 < option value = "review to write" > write reviews < / option >
 < option value = "write a review [100-yen discount]" > [100-yen discount] write a review < / option >
 < / select > 
 < / td > 
 < / tr > 
 < / tbody > ';

I need to format this table and assign into one div using Javascript or jQuery.

I have already tried trim() and html_entity_decode , but its not removed any unwanted spaces.

< table class = "buy_selection" >

How can i format variable. please advise.

Did you try to make a DOM element of it, and get its HTML back ?

var new_table = $("<div>"+str+"</div>"); // create a new DOM DIV containing the element with the given text

str_without_spaces = new_table.html();

But anyway, do you really need to remove the spaces ? I mean, when you use the first line above, it creates a DOM table where all the spaces disapear (since the spaces between "<" and ">" are removed, and the spaces insides tags are removed if they are not html code).

So the easiest is to create the DOM element with $(str); and append it to something in your html page.

I'm kinda bad at this, but this should do it

str = str.replace(/< /g, '<').replace(/<\/ /g, '</').replace(/ >/g, '>').replace(/\/ /g, '/')

FIDDLE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM