Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.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:IE中的对象问题 $('#historie.accordion_toggler.content').toggle(); 对于(i=0;i_Javascript_Jquery_Toggle - Fatal编程技术网

JavaScript/jQuery:IE中的对象问题 $('#historie.accordion_toggler.content').toggle(); 对于(i=0;i

JavaScript/jQuery:IE中的对象问题 $('#historie.accordion_toggler.content').toggle(); 对于(i=0;i,javascript,jquery,toggle,Javascript,Jquery,Toggle,我认为您可能正在对DOM元素本身调用该方法 $('#historie .accordion_toggler .content').toggle(); for (i=0; i<3; i++) { $('#historie .accordion_toggler .content')[i].toggle(); $('#historie .accordion_toggler .head')[i].toggleClass('active'); } 请尝试这样做,以确保只获取存在的元素,

我认为您可能正在对DOM元素本身调用该方法

$('#historie .accordion_toggler .content').toggle();
for (i=0; i<3; i++) {
   $('#historie .accordion_toggler .content')[i].toggle();
   $('#historie .accordion_toggler .head')[i].toggleClass('active');
}

请尝试这样做,以确保只获取存在的元素,并更新jQuery元素而不是本机DOM元素():

$('#historie.accordion_toggler.content').toggle();
var go_until=$('#historie.accordion_toggler.content').length;
if($('#historie.accordion_-toggler.head')。长度
出现错误的原因是数组中的元素不是jQuery对象,因此它们没有toggle方法

$('#historie .accordion_toggler .content').toggle();

var go_until = $('#historie .accordion_toggler .content').length;
if($('#historie .accordion_toggler .head').length < go_until){
   go_until = $('#historie .accordion_toggler .head').length;
}

for (var i = 0; i < go_until; i++) {
   // ANOTHER CHANGE HERE, that way you get the jQuery element 
   //  and not the DOM element:
   $('#historie .accordion_toggler .content').eq(i).toggle(); 
   $('#historie .accordion_toggler .head').eq(i).toggleClass('active');
}
这应该起作用:

$('#historie .accordion_toggler .content')[i].toggle();
但这是较短的:

$($('#historie .accordion_toggler .content')[i]).toggle();

即使您向我们展示一个完整的示例,也可能有更简单的方法。

您只有三个元素,还是只有前三个元素必须切换

您可以这样做:

$('#historie .accordion_toggler .content:lt(3)').toggle();
$('#historie .accordion_toggler .head:lt(3)').toggleClass('active');
对于前3个,您可以这样做

$('#historie .accordion_toggler .content').each(function(){
   $('this').toggle().toggleClass('active');
});
$('#historie.accordion_toggler.content')。每个(函数(i){
如果(i<3)
$('this').toggle().toggleClass('active');
});
您能在上发布一个演示,展示您目前的成果吗?
$('#historie .accordion_toggler .content').each(function(){
   $('this').toggle().toggleClass('active');
});
$('#historie .accordion_toggler .content').each(function(i){
   if(i < 3)
      $('this').toggle().toggleClass('active');
});