2012-05-08 100 views
-1

我的代码:如何使用类名隐藏DIV?

var array1 = document.getElementsByClassName("div"); 
var array2 = document.getElementsByClassName("button"); 

    for(var i = 0; i < array1.length; i++) 
    { 
     $(array2[i]).show(); 
     $(array1[i]).hide(); 

     $(array2[i]).click(function(){ 
      $(array1[i]).slideToggle(); 
     }); 
    } 

为什么我得到错误: 无法转换的JavaScript参数arg 0?

+1

发布[JS小提琴](http://jsfiddle.net/),或类似的,演示。否则,你不可能得到一个好的答案,因为我们不能看到你有什么问题。 –

+2

你是不是指'getElementsByTagName'?因为'div'和'buttons'是HTML中的标准标签。不上课。 –

+1

你很明显使用jQuery,你为什么混合? –

回答

4
var $buttons = $(".button").hide(); 

$(".div").show().bind("click", function(event) { 
    var index = $divs.index(this); 

    $buttons.eq(index).slideToggle(); 
}); 

OR:

var $buttons = $(".button").hide(), 
    $divs = $(".div").show(); 

$.each($buttons, function(index) { 
    var $button = $(this); 
    $divs.eq(index).bind("click", function() { 
     $button.slideToggle(); 
    }); 
}); 

OR

var $buttons = $(".button").hide(), 
    $divs = $(".div").show(); 

$buttons.each(function(index) { 
    var $button = $(this); 
    $divs.eq(index).bind("click", function() { 
     $button.slideToggle(); 
    }); 
}); 
+0

@Truth。谢谢你的className修复。 – andlrc

+0

我不介意提出这个问题,因为你已经实施了我的建议。 –

+0

我相信你不正确地使用index(),它应该是$('。div')。index($(this)); –