Верит малыш script
что обращается одни div
, проверь, если эти имеют одну из них # , пойдите , что есть в # array , и если это так, добавляет один div
внутри.
Я хочу сделать, что, если он не выполняет правило div
исчезните, но если я использую else{ $(this).remove()}
он удаляет все мне div
.
Когда я использую else{ $(this).addClass()
он применяет класс к тем, которые выполняют 1 условие, не в которые он не выполняет ее. Они знают, из-за которого он может быть?
var arreglo = ["A", "B", "c", "d","E","f","g","H"];
$(".sup > div").each(function(index){
console.log("comprobación "+index)
for(i=0; i< arreglo.length; i++ ){
var id_ = $(this).attr("id") //obtiene la #id del div
if(arreglo[i] === id_){//compara las #id si son iguales
console.log(arreglo[i]+" = "+id_+" BINGO")//lo declara en la consola
$(this).append("");//dibuja un div dentro
} else if(arreglo[i] !== id_){
console.log(arreglo[i]+" = "+id_+" NO")
$(this).toggleClass("class")
}
}//for2
}/*function*/)
Ты можешь упрощать достаточно cГіdigo. Чтобы подтверждать, если один пойдите, существуй в array ты можешь использовать mГ©todo some объекта Array. Начиная с ahГ, - если существует он aГ±ades div, и если не он aГ±ades класс, чтобы это скрывать:
var arreglo = ["A", "B", "c", "d","E","f","g","H"];
$(".sup > div").each(function(index){
var $div = $(this);
var id_ = $div.attr("id") //obtiene la #id del div
if (arreglo.some(function(element) { return element===id_; })){
$div.append("<div class='prueba' />");
}
else{
$div.addClass("oculto");
}
}/*function*/)
.prueba{
display:inline-block;
width:200px;
height:20px;
background:purple;
content:"Bingo";
}
.sup > div{
margin:5px;
}
.sup > div.oculto{
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sup">
<div id="A">1</div>
<div id="B">2</div>
<div id="C">3</div>
<div id="D">4</div>
<div id="F">5</div>
<div id="G">6</div>
<div id="H">7</div>
<div id="I">8</div>
</div>
Хорошие практики программирования начинают тем, что добавляют ;
после линии кода. Смоги, что ты if(){}
не функционируйте правильным способом ввиду этого.
var arreglo = ["A", "B", "c", "d","E","f","g","H"];
$(".sup > div").each(function(index){
console.log("comprobación "+index);
for(i=0; i< arreglo.length; i++ ){
var id_ = $(this).attr("id"); //obtiene la #id del div
if(arreglo[i] === id_){//compara las #id si son iguales
console.log(arreglo[i]+" = "+id_+" BINGO");//lo declara en la consola
$(this).append("<div class='prueba'/>");//dibuja un div dentro
} else if(arreglo[i] !== id_){
console.log(arreglo[i]+" = "+id_+" NO");
$(this).toggleClass("class");
}
}//for2
}/*function*/)
;
- рекомендуемый тот факт, что не функционировать, и # 225; программа по этой причине не является верной, так как переводчик javascript добавляет один ;
автоматически в конце каждый l и # 237; nea в случае, который не был бы определенным. Прочитай howtocreate.co.uk / tutorials / язык сценариев JavaScript / semicolons
– devconcept
04.05.2016, 19:57
Кроме ;
, проблема я это решил с переменной, которая проверяла бы, если существует id, и это подтверждала вне for
договоренности:
var arreglo = ["A", "B", "c", "d","E","f","g","H"];
$(".sup > div").each(function(index){
console.log("comprobación"+index);
var existe;
for(i=0; i< arreglo.length; i++ ){
var id_ = $(this).attr("id"); //obtiene la #id del div
existe = false;
if(arreglo[i] === id_){//compara las #id si son iguales
console.log(arreglo[i]+" = "+id_+" BINGO");//lo declara en la consola
$(this).append("<div class='prueba'/>");//dibuja un div dentro
existe = true; // seteo la variable para corroborar que si existe el id
break; //me salgo del for
}
}//for2
if(!existe) {
$(this).remove();
}else{
console.log(arreglo[i]+" = "+id_+" NO");
}
}/*function*/)