"未捕获的TypeError:undefined不是函数"jQuery的每个函数问题

bbo*_*tle 0 javascript jquery

这是一个非常基本的问题.我有这个代码块,我认为它可以,但它抛出一个错误......

Uncaught TypeError: undefined is not a function 
Run Code Online (Sandbox Code Playgroud)

这是有问题的代码

$('.colourbox').each(function(i){
// select visible children
var visibleDivs = $(this).children('div').length;

// use whatever width calculation you'd like...
var targetWidth = 300 / visibleDivs.length - 1;

// apply the width to the divs
visibleDivs.width(targetWidth);
});
Run Code Online (Sandbox Code Playgroud)

谢谢

Den*_*ret 6

visibleDivs 是一个数字:

var visibleDivs = $(this).children('div').length;
Run Code Online (Sandbox Code Playgroud)

因此它没有width使该行失败的功能:

visibleDivs.width(targetWidth);
Run Code Online (Sandbox Code Playgroud)

你可能想要

$('.colourbox').each(function(i){
   // select visible children
   var visibleDivs = $(this).children('div');

   // use whatever width calculation you'd like...
   var targetWidth = 300 / visibleDivs.length - 1;

   // apply the width to the divs
   visibleDivs.width(targetWidth);
});
Run Code Online (Sandbox Code Playgroud)

但您应该使用浏览器的开发人员工具自己解决此问题:

  1. 查看控制台中给出的错误的确切行
  2. 如果这还不够,调试和一行一行,查看变量的值