2013-10-02 19 views
5

musiałbym pętli wewnątrz niektórych elementów HTML, używając tego samego kodu pojawia się błądUncaught TypeError: Object # <HTMLDivElement> ma metoda „height” - nie można wywołać metodę na jQuery elementu

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

Co jest nie tak?

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

Odpowiedz

9

W swojej metodzie each(), this odnosi się do elementu DOM, a nie obiekt jQuery (stąd nie można wywołać metody jQuery na nim). Zamiast

elm = this 

Spróbuj

elm = $(this) 
2

Trzeba owinąć elementu DOM (this) do obiektu jQuery:

var elm = $(this); 

Zobacz 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.

Powiązane problemy