2013-10-02 11 views
5

nenne ich eine Schleife in einigen HTML-Elemente benötigen würde, mit dem gleichen Code ich einen FehlerUncaught Typeerror: Objekt # <HTMLDivElement> hat keine Methode ‚height‘ - Kann nicht jquery-Methode auf Element

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

Was ist hier falsch?

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)*$/, '...'); 
     }); 
    } 
    }) 

Antwort

9

In Ihrem each() Verfahren bezieht sich this auf das DOM-Element, kein jQuery-Objekt (daher können Sie nicht jQuery Methoden es nennen). Statt

elm = this 

Versuchen

elm = $(this) 
2

Sie müssen das DOM-Element (this) in ein jQuery-Objekt wickeln:

var elm = $(this); 

the docs of the .each() function Siehe:

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

Verwandte Themen