通过2个类和节点属性删除div [英] Delete divs by 2 classes and node attribute

查看:94
本文介绍了通过2个类和节点属性删除div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象我们有一些像这样的div

Imagine that we have some divs like that

<div class="row chapter" node="1">Chapter 1</div>
<div class="row section" node="1">Section 1</div>

Ajax接受必须删除的节点数组。我有几个关于这个问题。

Ajax accepts array of nodes that must be deleted. I got few questions about this

1)我使用以下函数:

1) I'm using following function

                $.each(result['nodes'], function(column, node)){
                   $(".row."+column).slideUp("slow").remove();
                }



我不知道如何删除2个类和节点属性。如何做?

I can't figure out how to delete by 2 classes and node atribute. How to do that?

2)AFAIK $ .each函数逐个删除。是否可以一次删除所有?
3)如何处理验证问题?我的意思是,

2) AFAIK $.each function deletes one by one. Is it possible to delete all at once? 3) How to deal with validation issues? I mean,

推荐答案

由于对象只有键 - 值对,为了处理两个类和一个属性,我们必须改变你的JSON的结构。我建议将其更改为[{class1:foo,class2:bar,node:1}]。使用该格式,这将工作收集节点:

Since an object only has key-value pairs, in order to handle two classes and one attribute, you'll have to change the structure of your JSON. I suggest changing it to [{"class1":"foo","class2":"bar","node":1}]. With that format, this will work to collect the nodes:

$($.map(result['nodes'], function(o) {
    return "."+o.class1+"."+o.class2+"[node="+o.node+"]";
}).join(","))

如Joseph所说,您的原始代码会过早删除节点,因此您需要 slideUp()的参数中的 remove()

As Joseph stated, your original code will remove the nodes too soon, so you'll need to put the remove() in a parameter to slideUp():

$($.map(result['nodes'], function(o) {
    return "."+o.class1+"."+o.class2+"[node="+o.node+"]";
}).join(",")).slideUp("slow", function() {
    $(this).remove()
});

示例jsFiddle: http://jsfiddle.net/tSu8z/

Sample jsFiddle: http://jsfiddle.net/tSu8z/

请注意,实际上没有一个接一个区别。使用上面的代码,jQuery获取顺序滑动节点一次,但内部它将迭代通过节点。同样,节点很可能都似乎是被同时删除,但jQuery实际上是通过它们迭代。它同时看起来的唯一要求是 slideUp 是异步的,它是。

Note that there's really no "one by one" vs. "all at once" distinction. With the code above, jQuery gets the order to slide the nodes up all at once, but internally it will iterate through the nodes. Likewise, the nodes will most likely all appear to be deleted simultaneously, but jQuery is actually iterating through them. The only requirement for it to look simultaneous is for slideUp to be asynchronous, which it is.

这篇关于通过2个类和节点属性删除div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆