Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将多个jQuery元素选择器传递给函数_Javascript_Jquery_Function_Jquery Selectors - Fatal编程技术网

Javascript 将多个jQuery元素选择器传递给函数

Javascript 将多个jQuery元素选择器传递给函数,javascript,jquery,function,jquery-selectors,Javascript,Jquery,Function,Jquery Selectors,myFunc()已绑定到文档滚动,因此将大量调用它。我想将HTML选择存储在一个var中,并将它们传递给函数。当我运行下面的示例时,我得到控制台错误无法获取属性“css”的值:对象为null或未定义 var a1 = $('#a1'); var a2 = $('#a2'); $(document).bind("scroll", function() { setTimeout(myFunc, 1000, a1, a2); } function myFunc(a1, a2) { a1.c

myFunc()已绑定到文档滚动,因此将大量调用它。我想将HTML选择存储在一个var中,并将它们传递给函数。当我运行下面的示例时,我得到控制台错误
无法获取属性“css”的值:对象为null或未定义

var a1 = $('#a1');
var a2 = $('#a2');

$(document).bind("scroll", function() {
  setTimeout(myFunc, 1000, a1, a2);
}

function myFunc(a1, a2) {
  a1.css('color', 'blue');
  a2.css('font-weight', 'bold');
}
如何将存储在变量中的多个jQuery选择器传递给函数?

请尝试以下操作:

$(document).bind("scroll", function() {
  setTimeout(function() {
      myFunc(a1, a2);
    },1000);
}); // close );

function myFunc(a1, a2) {
  a1.css('color', 'blue');
  a2.css('font-weight', 'bold');
}
$(document).bind("scroll", function() {
    setTimeout(function() {
        myFunc(a1, a2);
    }, 1000);
}); // and close properly your function
请尝试以下操作:

$(document).bind("scroll", function() {
    setTimeout(function() {
        myFunc(a1, a2);
    }, 1000);
}); // and close properly your function

您的
a1
a2
变量可以在页面上存在带有
#a1
#a2
的元素之前进行设置(特别是如果这些元素没有包装在onload/ready处理程序中,并且脚本位于页眉中)。我会这样设置,以确保在滚动事件发生时存在
#a1
#a2

var a1 = undefined;
var a2 = undefined;

$(document).bind("scroll", function() {
  if(typeof(a1) === "undefined") { a1 = $("#a1");} //will only reset a1 if undefined
  if(typeof(a2) === "undefined") {a2 = $("#a2");}

  setTimeout(function(){myFunc(a1,a2)}, 1000);
}); //don't forget your ')'

function myFunc(a1, a2) { 
  a1.css('color', 'blue');
  a2.css('font-weight', 'bold');
}

您的
a1
a2
变量可以在页面上存在带有
#a1
#a2
的元素之前进行设置(特别是如果这些元素没有包装在onload/ready处理程序中,并且脚本位于页眉中)。我会这样设置,以确保在滚动事件发生时存在
#a1
#a2

var a1 = undefined;
var a2 = undefined;

$(document).bind("scroll", function() {
  if(typeof(a1) === "undefined") { a1 = $("#a1");} //will only reset a1 if undefined
  if(typeof(a2) === "undefined") {a2 = $("#a2");}

  setTimeout(function(){myFunc(a1,a2)}, 1000);
}); //don't forget your ')'

function myFunc(a1, a2) { 
  a1.css('color', 'blue');
  a2.css('font-weight', 'bold');
}

还可以将元素存储在数组中:

var a1 = ['#a1','#a2'];

$(document).bind("scroll", function() {
    setTimeout(function() {
        myFunc(a1);
    }, 1000);
});

function myFunc(el) {
  $(el[0]).css('color', 'blue');
  $(el[1]).css('font-weight', 'bold');
}

还可以将元素存储在数组中:

var a1 = ['#a1','#a2'];

$(document).bind("scroll", function() {
    setTimeout(function() {
        myFunc(a1);
    }, 1000);
});

function myFunc(el) {
  $(el[0]).css('color', 'blue');
  $(el[1]).css('font-weight', 'bold');
}

OP的代码不应该做同样的事情吗(旧浏览器除外)?OP的代码不应该做同样的事情吗(旧浏览器除外)?