从数组的对象中删除所有标记和样式

remove all tags and styling from object of arrays

本文关键字:样式 删除 数组 对象      更新时间:2023-09-26

我正在从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、 amp;amp在将文本字段中的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} ));
 });

使用jQuery,可以通过以下方式实现:

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

因此,您用<div>包装text,并从中仅选择p

试试这个

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/