Try this instead:
$.fn.tagName = function() {
return this.get(0).tagName;
}
alert($('#testElement').tagName());
To explain a little bit more of why your original example didn't work, the each()
method will always return the original jQuery object (unless the jQuery object itself was modified). To see what is happening in each with your code, here is some pseudocode that shows how the each()
method works:
function each(action) {
for(var e in jQueryElements) {
action();
}
return jQueryObject;
}
This is not how each()
really gets implemented (by a long shot probably), but it is to show that the return value of your action()
function is ignored.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…