如何编写jquery插件来显示相关的子类别?

时间:2016-05-29 09:09:08

标签: php jquery

我在显示主要类别时出现问题,然后是子类别,然后是子类别,如下所示:

主要类别
 子类别
  子子类别
主要类别
 子类别
  子子类别

但现在它出现了这样:

主要类别
主要类别

子类别
子类别

子子类别
子子类别

这是我的插件脚本:

(function($){

$.fn.selection = function(options)
{
    var localThis = this;
    //var main = ['Arriving Soon'];
    var main = {'cat1':'a','cat2':'b','cat3':'c'};
    var divs;
    var sub;
    if(options){$.extend(main, options);}

    jQuery.each(main, function(index,value){
        //alert("value = "+index);
         divs = jQuery('<div />',{
            'text':index,
            'css':({
            'background-color':'yellow',
            'width':'100px',
            'height':'50px',
            'margin':'10px'})       
        }) .bind('click');
        jQuery.each(value, function(index2,value2){
            //alert(index2+" = "+value2);

       $(function(){
            sub = jQuery('<div />',{
            'text':index2+" [remove]",
            'css':({
            'background-color':'green',
            'width':'100px',
            'height':'50px',
            'margin':'10px'})
            });
            localThis.append(sub);
        }).bind('click',function()
        {
            sub2 = jQuery('<div />',{
            'text':value2+" [remove]",
            'css':({
            'background-color':'grey',
            'width':'100px',
            'height':'50px',
            'margin':'10px'})
            });
            localThis.append(sub2);
        });

        localThis.append(divs);


        });//inner loop

    });

};
})(jQuery);

使用/调用插件的脚本:

$(function()
{
    var a = [];
    a['cat1'] = {'test1':["sub1","sub2"],'test2':["sub3","sub4"]};
    a['cat2'] = {'test3':["sub5","sub6"],'test4':["sub7","sub8"]};
    a['cat3'] = {'test5':["sub9","sub10"],'test6':["sub11","sub12"]};
    $('div#main').selection(a);
});

HTML

<div id="main">

</div>

1 个答案:

答案 0 :(得分:1)

JS

(function($){

$.fn.selection = function(options)
{
    var localThis = this;
    //var main = ['Arriving Soon'];
    var main = {'cat1':'a','cat2':'b','cat3':'c'};
    var divs;
    var sub;
    if(options){$.extend(main, options);}

    jQuery.each(main, function(index,value){
        //alert("value = "+index);
         divs = jQuery('<div />',{
            'text':index,
            'css':({
            'background-color':'yellow',
            'width':'100px',
            'height':'50px',
            'margin':'10px'})       
        }).bind('click');
      localThis.append(divs);
      jQuery.each(value, function(index1, value1){
        sub = jQuery('<div />',{
            'text':index1+" [remove]",
            'css':({
            'background-color':'green',
            'width':'100px',
            'height':'50px',
            'margin':'10px'})
            });
            localThis.append(sub);
        jQuery.each(value1,function(index2,value2){
          sub = jQuery('<div />',{
            'text':value2+" [remove]",
            'css':({
            'background-color':'grey',
            'width':'100px',
            'height':'50px',
            'margin':'10px'})
            });
            localThis.append(sub);
        });
      });
    });
};
})(jQuery);


$(function()
{
    var a = {};
    a.cat1 = {'test1':["sub1","sub2"],'test2':["sub3","sub4"]};
    a.cat2 = {'test3':["sub5","sub6"],'test4':["sub7","sub8"]};
    a.cat3 = {'test5':["sub9","sub10"],'test6':["sub11","sub12"]};
    $('div#main').selection(a);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="main">

</div>