.width()错误$(...)[0] .width不是函数吗? [英] .width() giving error $(...)[0].width is not a function?

查看:222
本文介绍了.width()错误$(...)[0] .width不是函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子,我试图找到桌子上每个td的宽度.我尝试了以下各种变体:$("td")[0].width(),但是它们都不起作用,每次都会出现错误:"Uncaught TypeError: $(...)[0].width is not a function".

我先导入jQuery,再导入其他JS文件,( https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js ),而我使用的是.width()而不是.width.

有什么想法吗?

解决方案

$("td")[0]是DOM元素,而不是jquery对象.只需将其包装为jquery $($("td")[0]).width()

您需要获取每个td的宽度,以便可以使用

$.each('td', function() {
    var currentTDWidth = $(this).width(); // more code here
});

I have a table, and I'm trying to find the width of each td in the table. I've tried all kinds of variations of: $("td")[0].width(), but none of them work, getting an error of: "Uncaught TypeError: $(...)[0].width is not a function" everytime.

I'm importing jQuery first, before my other JS files, (https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js), and I'm using .width() not .width.

Any ideas?

解决方案

$("td")[0] is a DOM element not a jquery object. Simply wrap it as jquery $($("td")[0]).width()

You need to get width of each td so you could use something like

$.each('td', function() {
    var currentTDWidth = $(this).width(); // more code here
});

这篇关于.width()错误$(...)[0] .width不是函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆