JavaScript/jQuery:IE中的对象问题 $('#historie.accordion_toggler.content').toggle(); 对于(i=0;i
我认为您可能正在对DOM元素本身调用该方法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'); } 请尝试这样做,以确保只获取存在的元素,
$('#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');
});