[英]Checking children for attribute in conditional
I am trying to see if the <ul id="sortable">
has less than 3 <li>
children and each child does not contain the
qual="qualX"
(where 'X' is an integer and 'qual' is an attribute). 我试图查看
<ul id="sortable">
是否有少于3个<li>
子级,并且每个子级都不包含qual="qualX"
(其中'X'是整数,'qual'是属性)。
There is a 3x2 grid <div id="qualities">
that when a <div class="quality">
is clicked, it will add a <li>
to <ul id="sortable">
. 有一个3x2的网格
<div id="qualities">
,当单击<div class="quality">
,它将为<ul id="sortable">
添加一个<li>
<ul id="sortable">
。 The adding part works, but I want to make it so you can't add the same <li>
more than once and not have more than 3 <li>
total. 加法部分起作用,但是我想这样做,所以您不能多次添加相同的
<li>
并且总计不能超过3个<li>
。 Right now, only the 3 <li>
restricter is working; 目前,只有3个
<li>
限制器在起作用; the "don't add the samea thing twice" code is not, as it's always true right now (apparently). “不要两次添加相同的东西”代码不是,因为现在(显然)始终是正确的。
My logic code is 我的逻辑代码是
$(".quality").click(function() {
if($("#sortable").children().length < 3 && $("#sortable").children("li").each().attr("qual") !== $(this).attr("qual")) {
$("#sortable").append("<li qual=" + $(this).attr("qual") + ">" + $(this).text() + "</li>");
}
});
whereas the HTML that is using this is 而使用它的HTML是
<ul>
<li style="display:inline-block;">
<div id="qualities">
<div style="width:600px;overflow:hidden;background:blue;">
<div class="quality" qual="qual1" style="width:200px;float:left;">
Quality 1
</div>
<div class="quality" qual="qual2" style="width:200px;float:left;">
Quality 2
</div>
<div class="quality" qual="qual3" style="width:200px;float:left;">
Quality 3
</div>
<div class="quality" qual="qual4" style="width:200px;float:left;">
Quality 4
</div>
<div class="quality" qual="qual5" style="width:200px;float:left;">
Quality 5
</div>
<div class="quality" qual="qual6" style="width:200px;float:left;">
Quality 6
</div>
</div>
</div>
</li>
<li style="display:inline-block;">
<div id="priority">
<ul id="sortable">
<!--<li>Item 1</li>-->
</ul>
</div>
</li>
</ul>
<div id="priority">
<ul id="sortable">
<!--<li>Quality 1</li>-->
</ul>
</div>
... && !$("#sortable").children("[qual=" + $(this).attr('qual') + "]").length
This selects children that have the matching qual
attribute using the attribute selector. 这将使用属性选择器选择具有匹配
qual
属性的子级。
You should probably use data-qual
over qual
. 您可能应该使用
data-qual
不是qual
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.