开发者

How do I remove HTML and special charaters using jQuery?

So what I want to do is remove all of the HTML table tag elements, leaving the link tags alone.

<table border="0" cellpadding="1" cellspacing="0">
<tbody><tr valign="top"><td class="headlines">•</td><td class="headlines"><a href="" target="_top">SOME TEXT</a></td></tr>
</tbody></table>

It would be nice to wrap the linked text into a div or开发者_如何学JAVA place in a paragraph tag.

Any thoughts?


The question was changed to ask something completely different after this answer was posted.

See @T.J.'s answer for a solution to the new question.


Do it with — gasp, shock horror — vanilla JavaScript!

var $headlines = $('.headlines'),
    headlineText = $headlines.text(),
    replaceText = headlineText.replace(/•/g, '');

$headlines.text(replaceText);

or, slightly sexier syntax:

$('.headlines').text(function (index, text)
{
    return text.replace(/•/g, '');
});


Update: Woah, you completely changed the question with your edit. Completely.

You can process the tables in your new question, putting all the links contained by the table into a single new paragraph, like this:

$('table').each(function() {
  var $thisTable = $(this),
      links = $thisTable.find('a'),
      newPara;
  $thisTable.parent().append($("<p>").append(links));
  $thisTable.remove();
}); 

Live example

Or if you want each link in its own paragraph (probably better):

$('table').each(function() {
  var $thisTable = $(this),
      $parent = $thisTable.parent(),
      links = $thisTable.find('a');
  links.each(function() {
    $parent.append($("<p>").append(this));
  });
  $thisTable.remove();
}); 

Live example


Original answer, now irrelevant:

You can use empty:

$(".headlines").empty();

...to completely empty the matching elements.

Or if you want to selectively remove just the at the beginning, you can use html and pass in a function:

$(".headlines").html(function(index, html) {
    if (html.substring(0, 1) === "•") {
        html = html.substring(1);
    }
    return html;
});

Or perhaps the * and any whitespace after it:

$(".headlines").html(function(index, html) {
    return html.replace(/^• */, '');  // A `•` at start of string
                                      // followed by zero or more spaces
});

Live example

...but don't do either of the latter ones that if the headlines elements will have complex, nested structures with event handlers on them, etc. (it's fine for basic cells, though).


Your code works, but it will remove the entire element. empty will remove all child nodes including text or nested elements.

If you just wanted to remove all the text from elements containing dots you could do:

$(":contains('•')").text();

If you just wanted to get rid of the dots and leave everything else:

    $(":contains('•')").each(function() {
    var text = $(this).text();
    text.replace(/•/gi, ""))
    $(this).text(text);
    });


$("<div>"+$('table a').text()+"</div>").insertAfter('#whereveryouwant");


var $myTable = $('#myTableId'); //Add id="myTableId" to the table you want select
$myTable.replaceWith($myTable.children('a').wrapAll('<div></div>'));


$('.headlines a').parent().html();  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜