gpt4 book ai didi

javascript - 如果任何 Div 是空的做一些事情

转载 作者:太空狗 更新时间:2023-10-29 15:40:40 25 4
gpt4 key购买 nike

我有以下语句检查是否有任何带有 id #Drop 的 Div 为空,如果为空则显示警报。但是目前,当任何 div 中包含内容时,该语句将不再有效。

我想我想说的是我想要它,所以如果任何 div 为空,就会显示一个警告。总共有 4 个 Div,如果其中任何一个为空,则应显示警报消息,例如,如果 4 个中的 3 个具有内容,只要有一个空的 div,警报就会触发。

HTML:

 <div id="Drop1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="Drop2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="Drop3" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="Drop4" ondrop="drop(event)" ondragover="allowDrop(event)"></div>

JS:

$("#run").click(function(){
if ($("[id^='Drop']").html() === ""){
alert("empty")// ...
}
});

最佳答案

使用 jQuery :empty 选择器。阅读更多关于 :empty 的信息选择器。

Description: Select all elements that have no children (including text nodes).

检查 DEMO

 $("#run").click(function(){
if ($("[id^='Drop']:empty").length){
alert("empty")// ...
}
});

第二个选项:正如我在评论和@A 中提到的那样。沃尔夫 在此处的回答评论中提及我添加第二个选项

DEMO

$("#run").click(function(){       
if ($("[id^='Drop']").is(":empty")){
alert("empty")// ...
};
});

关于javascript - 如果任何 Div 是空的做一些事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29716260/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com