简体   繁体   English

使用JavaScript删除标记之间的每个空格

[英]Remove every white space between tags using JavaScript

I'm trying to remove white space between tags so that childNodes only contain those tags nodes not the white space nodes too. 我正在尝试删除标记之间的空白区域,以便childNodes仅包含那些标记节点,而不包含空白节点。 Here's my code : 这是我的代码:

<li>            
    <label for="firstName"  class="mainLabel">First Name : </label>                                 
    <input type="text" name="firstName" id="firstName"/>                                    
    <span>This must be filled</span>
</li>   

And here's the JS code : 这是JS代码:

var parentHTML = firstName.parentNode.innerHTML;
parentHTML = parentHTML.replace(/>\n</g,"><");
firstName.parentNode.innerHTML = parentHTML;

But when i alert parentHTML i get the same old string. 但是,当我提醒parentHTML我得到相同的旧字符串。

It's (not, see after the rule) because strings are immutable, I think, and you're setting the innerHTML of the parent element to be the exact same string you retrieved from it earlier. 它(不是,在规则之后看)因为字符串是不可变的,我认为,你将父元素的innerHTML设置为你之前从它检索的完全相同的字符串。

Instead, I'd suggest: 相反,我建议:

var firstname = document.getElementsByTagName('input')[0],
    parentHTML = firstname.parentNode.innerHTML,
    newHTML = parentHTML.replace(/\>\s+\</g,'');
firstname.parentNode.innerHTML = newHTML;

console.log(parentHTML, newHTML, (parentHTML == newHTML));

JS Fiddle demo . JS小提琴演示


With regards to the comment from jfriend00 (below), it seems the regular expression was the problem, the \\n didn't match the supplied pattern, that being the case, the following amendment satisfies teh requirements: 关于来自jfriend00(下面)的评论,似乎正则表达式是问题, \\n与提供的模式不匹配,在这种情况下,以下修订满足以下要求:

var firstname = document.getElementsByTagName('input')[0],
    parentHTML = firstName.parentNode.innerHTML;
parentHTML = parentHTML.replace(/>\s+</g, "><");
firstName.parentNode.innerHTML = parentHTML;

console.log(firstname, parentHTML);​

JS Fiddle demo . JS小提琴演示

References: 参考文献:

For most cases, I recommend removing space from: 对于大多数情况,我建议删除以下空间:

  • Beginning of document 文件的开头
  • End of document 文件结束
  • After > character >字符
  • Before < character 之前<人物

There are two cases I can think of where this will not do what you want, and these are the same two cases that impact the less aggressive solutions above. 有两种情况我可以想到这不会做你想做的事情,这两种情况会影响上面那些不那么激进的解决方案。

  • Empty space between inline-block elements is actually an intended or expected part of the layout. inline-block元素之间的空白空间实际上是布局的预期或预期部分。 If this space is collapsed to zero characters, the implicit space between elements is removed. 如果此空间折叠为零个字符,则会删除元素之间的隐式空间。 This can be avoided by changing my regex below to replace with a " " . 这可以通过更改我的正则表达式替换为" "来避免。

  • My original answer has been updated to preserve whitespace in <script> , <style> , <pre> , or <textarea> tags. 我的原始答案已更新,以保留<script><style><pre><textarea>标记中的空格。 All of these except <pre> are CDATA which means the content is not HTML and are parsed until the closing tag is found, which means the regex is a complete solution. <pre>之外的所有这些都是CDATA,这意味着内容不是HTML,并且在找到结束标记之前进行解析,这意味着正则表达式是一个完整的解决方案。 If a <pre> is nested or the white-space CSS property is used, this will not preserve your content. 如果嵌套了<pre>或使用了white-space CSS属性,则不会保留您的内容。

The solution: 解决方案:

    collapsed = expanded.replace(/(<(pre|script|style|textarea)[^]+?<\/\2)|(^|>)\s+|\s+(?=<|$)/g, "$1$3");

only spaces: 只有空格:

parentHTML = parentHTML.replace( new RegExp( "\>[ ]+\<" , "g" ) , "><" ); 

new line, tabs and spaces: 新行,制表符和空格:

parentHTML = parentHTML.replace( new RegExp( "\>[\s]+\<" , "g" ) , "><" ); 

https://regex101.com/r/sD7cT8/1 https://regex101.com/r/sD7cT8/1

Can you treat a html tag as a string in js? 你能把html标签视为js中的字符串吗? I guess it can be done. 我想可以做到。 try this! 试试这个!

s.replace(/\s+/g, ' ');

I came across this thread because I was searching for a solution to eliminate gaps around divs caused by white space in HTML source, or line feeds in my case. 我遇到了这个帖子,因为我正在寻找一种解决方案来消除由HTML源中的空格或我的情况下的换行引起的div之间的间隙。

Before I realized that white space could cause these gaps, I was going nuts trying to get rid of them. 在我意识到白色空间可能导致这些空白之前,我一直在努力摆脱它们。 I want to keep my HTML source formatted for readability, so compressing the code is not a good solution for me. 我想保持HTML源代码的格式化以便于阅读,因此压缩代码对我来说不是一个好的解决方案。 Even if I handled it this way, it doesn't fix divs that are generated by Google and other vendors. 即使我这样处理它,它也不会修复由Google和其他供应商生成的div。

I started by creating the following function and calling it in body onload. 我开始创建以下函数并在body onload中调用它。

function Compress_Html() {
    //Remove whitespace between html tags to prevent gaps between divs.
    document.body.innerHTML = document.body.innerHTML.replace( /(^|>)\s+|\s+(?=<|$)/g, "$1" );
}

This seemed to work perfectly, but unfortunately, it breaks the Google search box I have in my footer. 这似乎完美无缺,但不幸的是,它打破了我的页脚中的谷歌搜索框。

After trying many variations of the regex pattern without success, I found this regex tester at http://www.regexpal.com/ . 在尝试了许多正则表达式的变化后,我没有成功,我在http://www.regexpal.com/找到了这个正则表达式测试器。 I far as I can tell, the following pattern does what I need. 据我所知,以下模式可以满足我的需求。

( /(^|>)[ \n\t]+/g, ">" )

That said, the function was still breaking the search box. 也就是说,该功能仍在打破搜索框。 So I ended up moving it into a jQuery document ready function. 所以我最终将它移动到jQuery文档就绪函数中。 Now it's working and does not break the search box. 现在它正在工作,并没有打破搜索框。

<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
    $( document ).ready(function() {
        document.body.innerHTML = document.body.innerHTML.replace( /(^|>)[ \n\t]+/g, ">" );
    });
</script>

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

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