简体   繁体   English

如何使用Javascript删除html标记中的多余空格?

[英]How to remove unwanted spaces in html tags using Javascript?

I have one Javascript variable. 我有一个Javascript变量。 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. 我需要格式化该表并使用Javascript或jQuery分配到一个div中。

I have already tried trim() and html_entity_decode , but its not removed any unwanted spaces. 我已经尝试过trim()html_entity_decode ,但没有删除任何不需要的空格。

< 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 ? 您是否尝试过为其制作DOM元素,并取回其HTML?

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). 我的意思是,当您使用上面的第一行时,它会创建一个DOM表,其中所有空格都消失(因为“ <”和“>”之间的空格已删除,并且如果不是HTML代码,则内部标签的空格也已删除) 。

So the easiest is to create the DOM element with $(str); 因此,最简单的方法是使用$(str);创建DOM元素$(str); and append it to something in your html page. 并将其附加到您的html页面中。

I'm kinda bad at this, but this should do it 我对此很不好,但是应该这样做

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

FIDDLE 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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