是否可以使用 JQuery 删除 ' ' ..?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7589528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:02:11  来源:igfitidea点击:

Is it possible to remove '&nbsp' using JQuery..?

jqueryjquery-ui

提问by Akhil Paul

I have two buttons in a row and they are shown in two td's, and its not lined up correctly in IE, I doubt that hidden spaces(&nbsp) in the td's somewhere mayby after the input or before the input button, unfortunately I cant access the html code, its automatically generated. The only way is to control by using jQuery I have a jQuery something like this..

我连续有两个按钮,它们显示在两个 td 中,并且它在 IE 中没有正确排列,我怀疑 td 中的隐藏空格( )可能在输入之后或输入按钮之前,不幸的是我无法访问html 代码,它是自动生成的。唯一的方法是使用 jQuery 来控制我有一个像这样的 jQuery ..

$("td:contains(' ')").css("display", "none");

Is this a right way to get rid of that &nbsp in the td?

这是摆脱 td 中的   的正确方法吗?

回答by Ry-

No, you would do something along these lines:

不,您会按照以下方式做一些事情:

$("td").html(function (i, html) {
    return html.replace(/ /g, '');
});

回答by Beto Aveiga

NBSP is not an element, so you can't hide it.

NBSP 不是一个元素,所以你不能隐藏它。

What you could do is to rewrite the element which contains NBSP... like this:

你可以做的是重写包含 NBSP 的元素......像这样:

 $('#container').html( $('#container').html().split('&nbsp').join('') );

Also detecting where NBSP using jquery.contains and setting it's parent element a class with font size 0 or display none could do the trick.

还使用 jquery.contains 检测 NBSP 的位置并将其父元素设置为字体大小为 0 的类或显示 none 可以解决问题。

Note than if you choose to rewrite the HTML to erase unwanted NBSP all events previously attached will be removed.

请注意,如果您选择重写 HTML 以删除不需要的 NBSP,则之前附加的所有事件都将被删除。

回答by jbyrd

Rather than answering the OP's specific scenario, if we take the question title ("Is it possible to remove '&nbsp' using JQuery..?") and answer this in a general way (which I think is useful), then any answers using .html()have one fundamental flaw: replacing the html this way also removes any data and event handlers associated with the old html - not desirable! It may work for the OP's specific scenario, but I think it's useful to answer the question in a more general way...

而不是回答 OP 的特定场景,如果我们采用问题标题(“是否可以使用 JQuery 删除 ' ' ..?”)并以一般方式回答这个问题(我认为这很有用),那么使用任何答案.html()有一个基本缺陷:以这种方式替换 html 也会删除与旧 html 关联的任何数据和事件处理程序 - 不可取!它可能适用于 OP 的特定场景,但我认为以更一般的方式回答这个问题很有用......

The better way is to replace the .textContentof individual text nodes within the element. So given the following markup:

更好的方法是替换.textContent元素内的单个文本节点。因此,给出以下标记:

<div class="container">
    &nbsp;<a href="www.google.com">Here is a link &nbsp;</a>
    <p>Here is a paragraph with another link inside: <a href="www.facebook.com">&nbsp;&nbsp;another link&nbsp;</a>&nbsp;</p>
    And some final text &nbsp;&nbsp;&nbsp;&nbsp;
</div>

We can remove all &nbsp;(which is unicode \u00A0- see more about this here) like so:

我们可以删除所有&nbsp;(这是 unicode \u00A0-在此处查看更多相关信息),如下所示:

$('.container, .container *').contents().each(function() {
    if (this.nodeType === 3) { // text node
        this.textContent = this.textContent.replace(/\u00A0/g, '');
    }
});

$('.container, .container *')selects the container and all elements inside it, then .contents()selects all the children of those elements, including text nodes. We go through each of those nodes, and for every text node we encounter (nodeType === 3), we replace the &nbsp;(\u00A0) with an empty string.

$('.container, .container *')选择容器和其中的所有元素,然后.contents()选择这些元素的所有子元素,包括文本节点。我们遍历每个节点,对于遇到的每个文本节点 ( nodeType === 3),我们将&nbsp;( \u00A0) 替换为空字符串。

回答by user969931

Use this:

用这个:

var td = $("td:contains('&nbsp;')");
var html = td.html();
html = html.replace("&nbsp;","");
td.html(html);

回答by martifenosa

I've been struggling with this, at the end the solution was quite obvious, instead of using .html()had to use .text()

我一直在努力解决这个问题,最后解决方案很明显,而不是使用.html()必须使用.text()

Example:

例子:

var text = $(label).text();
    text.replace(/&nbsp;/gi,''); 

回答by vishwajit76

try this

尝试这个

$('#div').text($('#div').text().replace(/&nbsp;/g, ' '));