dongyi7041 2016-05-29 09:09
浏览 28
已采纳

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

I have an issue in displaying main category, followed by sub category then followed by sub category like this:

Main category
sub category
sub sub category
Main category
sub category
sub sub category

But Now it shows up like this:

Main category
Main category

sub category
sub category

sub sub category
sub sub category

Here's my script for plugin:

(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);

Script that make use/calling of the plugin:

$(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条回答 默认 最新

  • doutizong8099 2016-05-29 15:01
    关注

    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>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算