帮助简化jQuery

时间:2010-08-23 05:19:02

标签: jquery html

我怎样才能成为这名射手。

它应该做它应该但它很长。哦,我需要为每个号码运行一个函数。

这是我的HTML

<div id="moremenu" class="hide">
<ul>
<li>View count</li>
<li>Click count</li>
<li>Profile</li>
<li>Inbox</li>
<li>Uploads</li>
<li>TV</li>
<li>Notes</li>
<li>More</li>
</ul>
</div>
<div class="status"></div>

这是我的jQuery

    $("#moremenu ul li").click(function () {

    var index = $('#moremenu ul li').index(this);
    var clickval = $('#moremenu ul li').text(this);

    //$('.status').prepend('<p>click index was '+index+'</p>').fadeOut('fast').fadeIn('slow');
    if(index == 0){
        $('.status').prepend('<p>click index was '+index+'</p>').prepend('<p>You clicked '+clickval+'</p>').fadeOut('fast').fadeIn('slow');
    }
    if(index == 1){
        $('.status').prepend('<p>click index was '+index+'</p>').prepend('<p>You clicked '+clickval+'</p>').fadeOut('fast').fadeIn('slow');
    }
    if(index == 2){
        $('.status').prepend('<p>click index was '+index+'</p>').prepend('<p>You clicked '+clickval+'</p>').fadeOut('fast').fadeIn('slow');
    }
    if(index == 3){
        $('.status').prepend('<p>click index was '+index+'</p>').prepend('<p>You clicked '+clickval+'</p>').fadeOut('fast').fadeIn('slow');
    }
    if(index == 4){
        $('.status').prepend('<p>click index was '+index+'</p>').prepend('<p>You clicked '+clickval+'</p>').fadeOut('fast').fadeIn('slow');
    }
    if(index == 5){
        $('.status').prepend('<p>click index was '+index+'</p>').prepend('<p>You clicked '+clickval+'</p>').fadeOut('fast').fadeIn('slow');
    }
    if(index == 6){
        $('.status').prepend('<p>click index was '+index+'</p>').prepend('<p>You clicked '+clickval+'</p>').fadeOut('fast').fadeIn('slow');
    }
    if(index == 7){
        $('.status').prepend('<p>click index was '+index+'</p>').prepend('<p>You clicked '+clickval+'</p>').fadeOut('fast').fadeIn('slow');
    }
    if(index == 8){
        $('.status').prepend('<p>click index was '+index+'</p>').prepend('<p>You clicked '+clickval+'</p>').fadeOut('fast').fadeIn('slow');
    }
});

2 个答案:

答案 0 :(得分:4)

var index = $('#moremenu ul li').index(this);
var clickval = $('#moremenu ul li').text(this);

if(index >= 0 && index <= 8){
    $('.status').prepend('<p>click index was '+index+'</p>').prepend('<p>You clicked '+clickval+'</p>').fadeOut('fast').fadeIn('slow');
}

这是一个简单的JavaScript。

答案 1 :(得分:2)

    var index = $('#moremenu ul li').index(this);
    var clickval = $('#moremenu ul li').text(this);

    if(index >= 0 && index <= 8){
       $('.status').prepend('<p>click index was '+index+'</p>').prepend('<p>You clicked '+clickval+'</p>').fadeOut('fast').fadeIn('slow');
    }