2013-10-02 16 views
5

avrei bisogno di ciclo all'interno di alcuni elementi HTML, utilizzando lo stesso codice ottengo un erroreUncaught TypeError: Object # <HTMLDivElement> non ha un metodo 'height' - Impossibile chiamare il metodo jquery sull'elemento

jquery Uncaught TypeError: Object # has no method 'height'

Cosa c'è di sbagliato qui?

clamp: function() { 
    var elms = $('#wrapper .content ul li .title'); 
    elms.each(function(i) { 
    debugger 
    var elm = this; 
    var h = elm.height(); 
    var eO = elm.outerHeight(); 
    if (eO > h) { 
     elm.text(function(index, text) { 
     return text.replace(/\W*\s(\S)*$/, '...'); 
     }); 
    } 
    }) 

risposta

9

nel metodo each(), this riferisce all'elemento DOM, non un oggetto jQuery (e quindi non si può chiamare metodi jQuery su di esso). Invece di

elm = this 

Provate

elm = $(this) 
2

È necessario avvolgere l'elemento DOM (this) in un oggetto jQuery:

var elm = $(this); 

Vedi the docs of the .each() function:

More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element.

Problemi correlati