0

我从 API 获取一组对象:

var result = {
  [0]: {
    created_at: "2013-04",
    id: "444556663333",
    num_comments: 1,
    num_likes: 0,
    text: "<p>dfgg</p>",
    title: "title1",
    updated_at: "2013-04"
  },
  user: {
    first_name: "bob",
    id: "43633",
    last_name: "ddd"
  }
}

文本字段从网站引入原始格式。这包含一堆不同的标签,例如跨度和字符,例如 img、p、a、strong、em、br 和 & 我如何从对象中删除所有这些标签,除了文本字段中的 P 标签在我通过之前它变成下划线模板?

谢谢!

如何使用我的代码进行这项工作?不了解选择器的工作原理

var results = getCanvasPosts(CanvasID, {count: 75, start: ""}); //get posts from API

 $.when(results).done(function(posts){  
    var template = $("#template").html();

//Put function here?

    //merge template and data
    $("#target").html(_.template(template,{posts:posts} ));
 });
4

2 回答 2

1

Try This

var whitelist = "p"; // for more tags use the multiple selector, e.g. "p, img"
$("#text *").not(whitelist).each(function() {
    var content = $(this).contents();
    $(this).replaceWith(content);
});

http://jsfiddle.net/JEnvr/3/

于 2013-07-24T04:48:47.227 回答
1

With jQuery you could do that in this way:

var text = '<p>asdasd</p><div>asddsa</div>';

var only_p = $('p', $('<div>' + text + '</div>'));

So you wrap your text with <div> and select only p from it

于 2013-07-24T04:44:26.937 回答