admin管理员组

文章数量:1422029

I would like to create a tab view for my php application using Mootools. I have n number of tabs created from php script. My view is as follows.

<div>
<ul id="1">
    <li>Popular</li>
    <li>New Addition</li>
</ul>
</div>
<div>
<ul id="2">
    <li>Popular</li>
    <li>New Addition</li>
</ul>
</div>
...
<div>
<ul id="n">
    <li>Popular</li>
    <li>New Addition</li>
</ul>
</div>

How can apply style class active according to the clicks on Populalar or New Addition under each tabs.

Thanks

I would like to create a tab view for my php application using Mootools. I have n number of tabs created from php script. My view is as follows.

<div>
<ul id="1">
    <li>Popular</li>
    <li>New Addition</li>
</ul>
</div>
<div>
<ul id="2">
    <li>Popular</li>
    <li>New Addition</li>
</ul>
</div>
...
<div>
<ul id="n">
    <li>Popular</li>
    <li>New Addition</li>
</ul>
</div>

How can apply style class active according to the clicks on Populalar or New Addition under each tabs.

Thanks

Share Improve this question edited Jan 20, 2011 at 8:13 Sean Patrick Floyd 300k72 gold badges476 silver badges595 bronze badges asked Jan 20, 2011 at 7:52 AadiAadi 7,10928 gold badges102 silver badges148 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5
var tabs = document.getElements('li');
tabs.addEvent('click', function() {
    tabs.removeClass('active');
    this.addClass('active');
});

Try an example.

Here's how to do it in MooTools:

var activeElement = null;
$$('div ul li'
   /* you probably want a better selector,
      how about giving a parent element an id? */
  ).each(function(item){
    item.addEvent('click',function(event){
        $(event.target).addClass('active');
        if(activeElement != event.target){
            if(activeElement!=null)
                $(activeElement).removeClass('active');
            activeElement = event.target;
        }
    });
});

Update: Here's an improved version, thanks @steweb, source:

$$('#containerID li').each(function(item){
    item.addEvent('click',function(event){
        // minor improvement to steweb's code,
        // restrict to .active inside container
        $$('#containerID .active').removeClass('active');
        this.addClass('active');
    });
});

It requires the root div to have the id "containerId":

<div id="containerId">
<ul id="1">
<!-- etc -->

object.className = 'active';

( Where object is what you want to highlight )

本文标签: How can I activate a style class on click using Mootools or JavascriptStack Overflow