根据最近的一些评论,我冒险尝试并开始使用 jquery。 我真的处于非常基础的水平,所以要友善。如果没有,请点击离开页面并回答其他人,厌倦了被标记。
好的,我有一些搜索结果,我们正在 sdebar 中回显向下钻取条件,以便用户可以修改他们的搜索。所以想添加用户单击图标的功能(设置为按钮) 我已经一切正常,但是,不知道如何设置它,以便每个元素都是可删除的,而不生成大量代码(jquery ids 等)
fiddle :http://jsfiddle.net/ozzy/sKeW5/
js:
$("input").click(function () {
$("div").remove('.dave');
});
CSS:
input[type=button]{background: url('dx.png');outline:none;border:none;background-repeat:no-repeat;cursor:pointer;width:11px;height:11px;}
html:
<div class="s_total clearfix dave"><strong class="cart_module_search left">State:</strong><span class="cart_module_search">Queensland </span><input type="button"/></div>
<div class="s_total clearfix"><strong class="cart_module_search left">Region:</strong><span class="cart_module_search">Brisbane </span><input type="button"/></div>
<div class="s_total clearfix"><strong class="cart_module_search left">Category:</strong><span class="cart_module_search">Home and Garden </span><input type="button"/></div>
<div class="s_total clearfix"><strong class="cart_module_search left">Search Term:</strong><span class="cart_module_search">Weber BBQ </span><input type="button"/></div>
正如您所看到的,我已将 dave 附加到类中的第一个 div,但想知道在仍保留对每个 div 元素的控制的同时,更简洁的方法是什么。我可以给每个 div 一个 id,但又不想生成大量的 js 代码。
最佳答案
更改:
$("input").click(function () {
$("div").remove('.dave');
});
至
$("input").click(function () {
$(this).parent().remove();
});
所以基本上,如果您在匿名 jquery 函数中调用它,它会指向调用它的对象,在本例中是您的按钮。然后,您获取按钮的父级并将其删除。
关于jquery - 删除div onclick(使用remove),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9272106/