从$(this).html()中按类删除span

时间:2012-12-24 08:59:01

标签: html ajax

基本上我的搜索以跨度返回搜索结果,然后当单击一个时,新的跨度将添加到具有选择输入和隐藏输入的另一个div中,因此所有选定的要素都可以作为数组发布。

我的问题是,$(this).html()现在包含了一个班级alias_span。我不希望出现在新的范围内。如何在将单击的范围内容插入新范围

之前将其删除
$(".minisearch_res").live('click', function() { 

    // when a search result is clicked we disable the submit button, 
    // append a span to the left column with an id, 
    // a select input to select standard/optional and
    // a hidden field with the required information to save and something to 
    // show the user


    var id = $(this).attr('id');

    var html = "<span class= \"added_result_cont\" id=\"" + id + "_cont\">";
    html += "<select name='" + id + "_sel'>";
    html += "<option value=\"none\" selected=\"selected\"></option>";
    html += "<option value=\"std\">Standard</option>";
    html += "<option value=\"opt\" >Optional</option>";
    html += "</select>";
    html += "<span id= \"" + id + "\" class=\"added_result\">";
    html += "<input type=\"hidden\" name=\"selectedfeat[]\" value=\"" + id + "\">";
    html += $(this).html() + "</span></span>";

    $('#div_apply_to').append(html);

    $(this).remove();
    $('#search_input').trigger('keyup');
    $("input[type=submit]").attr("disabled", "disabled");
});

更新:这是跨度的html

<span class="minisearch_res" id="opt_1">Anti-Lock Brakes<br><span style="padding-left:20px" class="alias_span"><i>abs</i></span><br></span>

2 个答案:

答案 0 :(得分:0)

<div></div>

将其添加到dom中,然后将其删除。

var htm = (
    '<span class="foo">foo</span>' +
    '<span>bar</span>' +
    '<span>bar</span>' +
    '<span>bar</span>' +
    '<span>bar</span>'
);

var $el = $("div").append(htm);
console.log($el.html());
$el.find('.foo').remove();
console.log($el.html());

http://jsfiddle.net/chovy/w2HdE/

答案 1 :(得分:0)

有一个整洁的跨浏览器解决方案,我为此感到非常自豪:)

var temp = $(this);
temp.children('.alias_span').remove();
 $('#div_apply_to').append("...html..." + temp.html() + "...html...");

灵感来自这里: Remove element from Ajax Response