Javascript 对象不支持jQuery原型上定义的函数的此属性或方法
我正在为sane浏览器提供的各种现成功能构建一个IE垫片(不幸的是,我处于一个必须支持IE7的位置) 我在jQuery原型上定义了以下方法定义:Javascript 对象不支持jQuery原型上定义的函数的此属性或方法,javascript,jquery,Javascript,Jquery,我正在为sane浏览器提供的各种现成功能构建一个IE垫片(不幸的是,我处于一个必须支持IE7的位置) 我在jQuery原型上定义了以下方法定义: $.fn.textWidth = function(){ //some logic in here } shimSelect = function(selector){ $(selector).on('focus', function(){ $(this).each(fu
$.fn.textWidth = function(){
//some logic in here
}
shimSelect = function(selector){
$(selector).on('focus', function(){
$(this).each(function(index, element){
if(element.textWidth() > element.width){
//more logic
}
}
}
}
if (!$.support.leadingWhitespace) {
$(function() {
shimSelect("");
});
}
通过明智地添加各种console.log,我已经确定在调用textWidth之前,元素是一个HTMLElement(具体地说是HTMLSelectElement)
但是,它拒绝调用textWidth(),失败原因是对象不支持IE和中的此属性或方法
Uncaught TypeError: Object #<HTMLSelectElement> has no method 'textWidth' shim.js:104
(anonymous function) togo_ie_shim.js:104
jQuery.extend.each jquery.js:612
jQuery.fn.jQuery.each jquery.js:242
(anonymous function) togo_ie_shim.js:103
jQuery.event.dispatch jquery.js:3064
elemData.handle.eventHandle
uncaughttypeerror:Object#没有方法'textWidth'shim.js:104
(匿名函数)togo_ie_shim.js:104
jQuery.extend.each jQuery.js:612
jQuery.fn.jQuery.each jQuery.js:242
(匿名函数)togo_ie_shim.js:103
jQuery.event.dispatch jQuery.js:3064
elemData.handle.eventHandle
论铬
我的问题是,我的代码有什么问题?为什么会失败?我怎样才能正确地实现类似的垫片呢?该函数没有将jquery对象作为参数提供给您,它是一个DOM对象
jQuery.each( collection, callback(indexInArray, valueOfElement) )
您已经扩展了jquery对象,因此,元素没有定义函数textWidth
尝试改用:$(element)
。没有方法“textWidth”
如果我正确阅读jQuery,element
是实际的DOM元素,它没有textWidth
属性。请尝试$(元素).textWidth()
。