Leí Jquery: Cómo verificar si el elemento tiene cierta clase/estilo css y funcionó -
if ($(".box").css('display') == 'block') { console.log("message 2 - Div with box class exists and css Block exists") }
Leí cómo crear jquery deslizar hacia la izquierda y hacia la derecha y conseguí que funcionara:
$(".box").animate({width: "toggle"});
Sin embargo, parece que no puedo combinar los dos anteriores. Estos dos intentos fallaron -
// ($(".box").css('display') == 'block').animate({width: "toggle"}); // ($(".box").css('display') == 'block').animate({width: "toggle"}); var datebox = ($(".box").css('display') == 'block') datebox.animate({width: "toggle"}) datebox.animate({width: "toggle"})
Actualización 1
Intenté la sugerencia de Cucunber y obtuve este error: Uncaught TypeError: ".box".each
no es una función
Actualización 2
Resolví el problema resaltado en la Actualización 1 agregando $
a ('.box')
, Cucunber actualizó su respuesta a continuación en función del problema que enfrenté.
Actualización 3
Jugué con la solución de Cucunber y eventualmente resolví mi problema con este código.
$(".box" ).each(function( index ) { if ($(this).css('display') == 'block' ) { console.log( index + ": " + $( this ) ); } })
La oración '$('.box').css('display') == 'block'
devuelve una declaración booleana. Si lo sabe, el método .animate
debe usarse con el elemento html (selector). Intenta usar algo como esto:
'$('.box').css('display') == 'block' && $('.box').animate({width: 'toggle'})
combínelo con '.each()'
y el resultado final será:
$('.box').each(function(){ $(this).css('display') == 'block' && $(this).animate({width: 'toggle'}) })