改变mootools功能,id到类?

时间:2013-03-07 14:39:41

标签: mootools

我有一个分配给id的函数(#item& #content)。 但是我怎样才能重写它,以便它适用于类?

window.addEvent('domready', function() {

    var myVerticalSlide = new Fx.Slide('content');

    $('item').addEvent('click', function(event){
        event.stop();
        myVerticalSlide.toggle();
    });
});

2 个答案:

答案 0 :(得分:0)

如果我理解正确,“项目”的计数和“内容”的计数将是相同的。尝试类似的东西

<script>
    window.addEvent('domready', function() {
        $$('.item').each(function(el,ind){
            new Fx.Slide($$('.content')[ind]).hide();
            el.addEvent('click', function(event){
                event.stop();
                var myVerticalSlide = new Fx.Slide($$('.content')[ind]).toggle();
            }); 
        })
    });
</script>

答案 1 :(得分:0)

以下是整个代码:

    <style type="text/css">
    .wrapper{
        max-width:613px;
        float:left;
        position:relative;
    }
    .new { 
        border: solid 1px #ddd; 
        padding: 5px; 
        float:left;
        max-width:660;
        min-width:320px;
    }
    .item{
        width:120px;
        padding:10px;
        display:block;
        background-color:#CCC;
        float:left;
        margin:5px;
    }
    .content{
        padding:10px;
        height:80px;
        visibility:hidden;
    }
    .item:hover > .content{

    }
    .full{
        position:absolute;
        width:100%;
        display:block;
        height:100px;
        background-color:#666;
        left:0px;
    }
  </style>

<script type='text/javascript'>//<![CDATA[ 

window.addEvent('load', function() {
(function(){
    var wrap,
        overview = document.getElement('div.overview');

    overview.getElements('div.item').each(function(a, i){
        if (i % 4 == 0){
            wrap && overview.adopt(wrap);
            wrap = new Element('div.new');
        }
        wrap.adopt(a);
    });

    overview.adopt(wrap);
}());
});
window.addEvent('domready', function() {
        $$('.item').each(function(el,ind){
            new Fx.Slide($$('.content')[ind]).toggle();
            el.addEvent('click', function(event){
                event.stop();
                var myVerticalSlide = new Fx.Slide($$('.content')[ind]).toggle();
            }); 
        })
    });//]]>  
</script>
</head>
<body>
<div class="wrapper">
    <div class="overview">
        <div  class="item">Test1
        <div  class="content">
            <div class="full">
                <p>Content1</p>
            </div>
        </div>
        </div>
        <div class="item">Test2
        <div class="content">
            <div class="full">
                <p>Content2</p>
            </div>
        </div></div>
        <div class="item">Test3
        <div class="content">
            <div class="full">
                <p>Content3</p>
            </div>
        </div></div>
        <div class="item">Test4
        <div class="content">
            <div class="full">
                <p>Content4</p>
            </div>
        </div></div>
        <div class="item">Test5
        <div class="content">
            <div class="full">
                <p>Content5</p>
            </div>
        </div></div>
        <div class="item">Test6
        <div class="content">
            <div class="full">
                <p>Content6</p>
            </div>
        </div></div>
        <div class="item">Test7
        <div class="content">
            <div class="full">
                <p>Content7</p>
            </div>
        </div></div>
        <div class="item">Test8
        <div class="content">
            <div class="full">
                <p>Content8</p>
            </div>
        </div></div>
        <div class="item">Test9
        <div class="content">
            <div class="full">
                <p>Content9</p>
            </div>
        </div></div>
        <div class="item">Test10
        <div class="content">
            <div class="full">
                <p>Content10</p>
            </div>
        </div></div>
        <div class="item">Test11
        <div class="content">
            <div class="full">
                <p>Content11</p>
            </div>
        </div></div>
    </div>
</div>

另一件事是,当我点击第一个div时,它会打开,之后,当我点击它打开的第二个div时,但是当我点击第二个div时,第一个div应该关闭。