Jquery说该对象不支持“每个”

Jquery saying that object doesn't support 'each'

本文关键字:每个 不支持 对象 Jquery      更新时间:2023-09-26

当我运行我的 Web 应用程序时,我收到一条错误消息,指出"JavaScript 运行时错误:对象不支持属性或方法'每个'",我正在尝试在我的 jquery 滑块下方或上方创建一个图例......

这是我正在使用的脚本

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />

这是我的 JQuery 脚本

$(function () {
      $("#slider-range").slider({
          range: true,
          min: 0,
          max: 100,
          values: [0, 100],
          animate: 'slow',
                        slide: function (event, ui) {
              //$(ui.handle).find('span').html('$' + ui.value);
              //
              $("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);
                            }
          .each(function() {
                                // Get the options for this slider
              var opt = $(this).data().uiSlider.options;
              // Get the number of possible values
              var vals = opt.max - opt.min;
              // Space out values
              for (var i = 0; i <= vals; i++) {
                  var el = $('<label>'+(i+1)+'</label>').css('left',(i/vals*100)+'%');
                  $( "#slider" ).append(el);
              }
          })
      });
      $("#amount").val("$" + $("#slider-range").slider("values", 0) +
        " - $" + $("#slider-range").slider("values", 1));
  });

任何帮助将不胜感激。

谢谢

您正在对选项对象应用each,该对象只有rangeminmaxvaluesanimate。确实,没有each.

您正在对幻灯片处理程序函数调用each() - 放错了位置})

$("#slider-range").slider({
    range: true,
    min: 0,
    max: 100,
    values: [0, 100],
    animate: 'slow',
    slide: function (event, ui) {
        //$(ui.handle).find('span').html('$' + ui.value);
        //
        $("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);
    }// <-- you had it here
}).each(function () {
    // Get the options for this slider
    var opt = $(this).data().uiSlider.options;
    // Get the number of possible values
    var vals = opt.max - opt.min;
    // Space out values
    for (var i = 0; i <= vals; i++) {
        var el = $('<label>' + (i + 1) + '</label>').css('left', (i / vals * 100) + '%');
        $("#slider").append(el);
    }
});

你在错误的地方调用.each(),你需要做}).each(function () {

$(function () {
    $("#slider-range").slider({
        range: true,
        min: 0,
        max: 100,
        values: [0, 100],
        animate: 'slow',
        slide: function (event, ui) {
            //$(ui.handle).find('span').html('$' + ui.value);
            //
            $("#amount").val("$" + ui.values[0] + " - $" + ui.values[1]);
        }
    }).each(function () { //you each loop like this
        // Get the options for this slider
        var opt = $(this).data().uiSlider.options;
        // Get the number of possible values
        var vals = opt.max - opt.min;
        // Space out values
        for (var i = 0; i <= vals; i++) {
            var el = $('<label>' + (i + 1) + '</label>').css('left', (i / vals * 100) + '%');
            $("#slider").append(el);
        }
    })
});