Is it possible to remove ' ' using JQuery..?
I have two buttons in a r开发者_开发百科ow and they are shown in two td's, and its not lined up correctly in IE, I doubt that hidden spaces( ) 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:contains(' ')").css("display", "none");
Is this a right way to get rid of that   in the td?
No, you would do something along these lines:
$("td").html(function (i, html) {
return html.replace(/ /g, '');
});
Rather than answering the OP's specific scenario, if we take the question title ("Is it possible to remove ' ' 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...
The better way is to replace the .textContent
of individual text nodes within the element. So given the following markup:
<div class="container">
<a href="www.google.com">Here is a link </a>
<p>Here is a paragraph with another link inside: <a href="www.facebook.com"> another link </a> </p>
And some final text
</div>
We can remove all
(which is unicode \u00A0
- see more about this here) like so:
$('.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
(\u00A0
) with an empty string.
NBSP is not an element, so you can't hide it.
What you could do is to rewrite the element which contains NBSP... like this:
$('#container').html( $('#container').html().split(' ').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.
Note than if you choose to rewrite the HTML to erase unwanted NBSP all events previously attached will be removed.
Use this:
var td = $("td:contains(' ')");
var html = td.html();
html = html.replace(" ","");
td.html(html);
I've been struggling with this, at the end the solution was quite obvious, instead of using .html()
had to use .text()
Example:
var text = $(label).text();
text.replace(/ /gi,'');
try this
$('#div').text($('#div').text().replace(/ /g, ' '));
精彩评论