Estoy tratando de obtener todos los valores de compensación () .top de cada elemento $('.example p')
console.log($('.example p')[1].offset().top)TypeError no detectado: $(...)[1].offset no es una función
Probé un montón de cosas, como usar otros selectores, usar un bucle for, foreach... Solo puedo obtener el valor offset().top del primer elemento de esa matriz eliminando [1], para el segundo , tercero, etc... elemento Parece que no puedo entenderlo.
Porque $('.example p')[1] nodo de retorno HTML (elemento html)
Si desea obtener una compensación con jquery, puede usar: $('.example p').eq(1).offset()
Simplemente itere a través de cada uno de los elementos en el selector ".example p" usando each
Ver demostración:
$(".example p").each(function(i) { console.log( $(this).offset().top); }); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="example"> <p>Doggo ipsum you are doing me a frighten corgo heckin good boys and girls pupperino, fat boi. Doing me a frighten you are doing me the shock clouds you are doin me a concern borkf super chub, wow very biscit you are doing me a frighten pupper. you are doing me a frighten pupper porgo. Fluffer wow very biscit blop fluffer puggorino puggo very hand that feed shibe, boofers maximum borkdrive the neighborhood pupper boofers. </p> <p>Doggo ipsum you are doing me a frighten corgo heckin good boys and girls pupperino, fat boi. Doing me a frighten you are doing me the shock clouds you are doin me a concern borkf super chub, wow very biscit you are doing me a frighten pupper. you are doing me a frighten pupper porgo. Fluffer wow very biscit blop fluffer puggorino puggo very hand that feed shibe, boofers maximum borkdrive the neighborhood pupper boofers. </p> </div> <div class="something"> <p>Doggo ipsum you are doing me a frighten corgo heckin good boys and girls pupperino, fat boi. Doing me a frighten you are doing me the shock clouds you are doin me a concern borkf super chub, wow very biscit you are doing me a frighten pupper. you are doing me a frighten pupper porgo. Fluffer wow very biscit blop fluffer puggorino puggo very hand that feed shibe, boofers maximum borkdrive the neighborhood pupper boofers. </p> <p>Doggo ipsum you are doing me a frighten corgo heckin good boys and girls pupperino, fat boi. Doing me a frighten you are doing me the shock clouds you are doin me a concern borkf super chub, wow very biscit you are doing me a frighten pupper. you are doing me a frighten pupper porgo. Fluffer wow very biscit blop fluffer puggorino puggo very hand that feed shibe, boofers maximum borkdrive the neighborhood pupper boofers. </p> </div> <div class="example"> <p>Doggo ipsum you are doing me a frighten corgo heckin good boys and girls pupperino, fat boi. Doing me a frighten you are doing me the shock clouds you are doin me a concern borkf super chub, wow very biscit you are doing me a frighten pupper. you are doing me a frighten pupper porgo. Fluffer wow very biscit blop fluffer puggorino puggo very hand that feed shibe, boofers maximum borkdrive the neighborhood pupper boofers. </p> <p>Doggo ipsum you are doing me a frighten corgo heckin good boys and girls pupperino, fat boi. Doing me a frighten you are doing me the shock clouds you are doin me a concern borkf super chub, wow very biscit you are doing me a frighten pupper. you are doing me a frighten pupper porgo. Fluffer wow very biscit blop fluffer puggorino puggo very hand that feed shibe, boofers maximum borkdrive the neighborhood pupper boofers. </p> </div>Parece que jQuery no devuelve una matriz de objetos jQuery. Compruebe, por ejemplo, si $('.example p')[1] es un objeto jQuery. Debe convertir todos los elementos en un objeto jQuery de esta manera:
var allExample = $('.example p').map((i, e) => $(e))Y luego vuelve a intentarlo:
console.log(allExample[1].offset())