开发者

jQuery remove the prev occurance of a class?

开发者 https://www.devze.com 2022-12-14 17:20 出处:网络
I have a class that I want to dynamically remove based on a link.How do I select the parent \"added\" class with jQuery.There can be multibpule instances of the class I only want to remove the closest

I have a class that I want to dynamically remove based on a link. How do I select the parent "added" class with jQuery. There can be multibpule instances of the class I only want to remove the closest parent.

Example:

<div class="added">
    <div class="cl">&nbsp;</div>
        <label>Add Item<br />
            <span>What to Add</span>
        </label>
        <div class="rightbox">
            <% fie开发者_StackOverflow中文版lds_for "items[related_items][]", add do |item_form|%>
                <%= item_form.text_field :add %>
            <%= link_to_function "remove", **"('div.added').empty()"** %>
            <% end %>       
        </div>
    <div class="cl">&nbsp;</div>
</div>

<div class="added">
    <div class="cl">&nbsp;</div>
        <label>Add Item<br />
            <span>What to Add</span>
        </label>
        <div class="rightbox">
            <% fields_for "items[related_items][]", add do |item_form|%>
                <%= item_form.text_field :add %>
            <%= link_to_function "remove", **"('div.added').empty()"** %>
            <% end %>       
        </div>
    <div class="cl">&nbsp;</div>
</div>

<div class="added">
    <div class="cl">&nbsp;</div>
        <label>Add Item<br />
            <span>What to Add</span>
        </label>
        <div class="rightbox">
            <% fields_for "items[related_items][]", add do |item_form|%>
                <%= item_form.text_field :add %>
            <%= link_to_function "remove", **"('div.added').empty()"** %>
            <% end %>       
        </div>
    <div class="cl">&nbsp;</div>
</div>


If you want closest parent and the link is inside the relevant div then:

$("a.classname").click(function() {
  $(this).closest("div.added").removeClass("added");
  return false;
});

I'm not certain that's relevant to what you're doing as I'm not familiar with ASP.NET but the above is the vanilla jQuery way of doing it.


Never mind just figured it out.... see below for anyone viewing this question.

$(this).closest('.added').empty()
0

精彩评论

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