简体   繁体   English

如何用隐藏的unicode字符消除Dom中的空白?

[英]how do you eliminate white space from the Dom with hidden unicode characters?

JSFiddle: http://jsfiddle.net/QbyUR/ JSFiddle: http : //jsfiddle.net/QbyUR/

So, I have an empty TD, that I'm trying to remove the white space from. 因此,我有一个空的TD,试图从中删除空白。

Normally I'd do this: 通常我会这样做:

ee.replace(/\s+/, '') == "" // check to see if TD is empty.

the above returns false 以上返回false

but, it wasn't working so I pasted the contents of my TD to a unicode decoder and got this: 但是,它没有用,所以我将TD的内容粘贴到unicode解码器中,并得到以下信息:

U+000D <control> character
U+000A <control> character
U+0009 <control> character
U+0009 <control> character
U+0009 <control> character
U+0009 <control> character
U+0020 SPACE character

or 要么

&#x000d;&#x000a;&#x0009;&#x0009;&#x0009;&#x0009;&#x0020;

The text that I converted is here: 我转换后的文字在这里:

-------


-------

(in between the two lines) (在两行之间)

this is what I used: http://software.hixie.ch/utilities/cgi/unicode-decoder/utf8-decoder 这就是我使用的: http : //software.hixie.ch/utilities/cgi/unicode-decoder/utf8-decoder

I'm 100% certain it's the control characters that are messing me up. 我100%肯定是控制字符弄乱了我。 And they are bound to be different across all browsers... 而且它们在所有浏览器中注定是不同的...

How do I get rid of them? 我如何摆脱它们? is a regex replace going to be sufficient? 正则表达式替换就足够了吗?

The real problem here is that you're using innerText , which is not a property on jQuery objects but rather on DOM elements. 真正的问题是您使用的是innerText ,它不是jQuery对象的属性,而是DOM元素的属性。 Simply use the jQuery function .text() instead: 只需使用jQuery函数.text()

var e = $(".td");
var result = e.text().replace(/\s+/, '') == ""; // check to see if TD is empty

alert(result);​

And everything will work fine. 一切都会正常。 Right now, the string is actually "undefined" because you appended an empty string to a nonexistent property. 现在,该字符串实际上是"undefined"因为您将一个空字符串附加到了不存在的属性中。

What you are trying to do will work correctly, but you wrote your Javascript wrong. 您尝试执行的操作将正常运行,但是您编写的Javascript错误。 Since you're using jQuery to select the element, you need to get the first index to access the innerText of the element. 由于您使用的是jQuery选择元素,因此需要获取第一个索引才能访问元素的innerText

http://jsfiddle.net/QbyUR/5/ http://jsfiddle.net/QbyUR/5/

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

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