0

我试图让 jQuery 根据两个值之间的属性值显示/隐藏元素。

我有如下标记:

<a class="page_link" href="javascript:go_to_page(0)" longdesc="0">1</a>
<a class="page_link" href="javascript:go_to_page(1)" longdesc="1">2</a>
<a class="page_link" href="javascript:go_to_page(2)" longdesc="2">3</a>
<a class="page_link" href="javascript:go_to_page(3)" longdesc="3">4</a>
<a class="page_link active_page" href="javascript:go_to_page(4)" longdesc="4">5</a>
<a class="page_link" href="javascript:go_to_page(5)" longdesc="5">6</a>
<a class="page_link" href="javascript:go_to_page(6)" longdesc="6">7</a>
<a class="page_link" href="javascript:go_to_page(7)" longdesc="7">8</a>
<a class="page_link" href="javascript:go_to_page(8)" longdesc="8">9</a>

我有一些看起来像这样的 jQuery:

//Now, we need to paginate the pagination
var page_link_count = $('.page_link').length(); 

//Hide all the .page_links
$('.page_link').css('display','none'); 

//Show active_page
$('.active_page').css('display','block'); 

//Show up to four .page_link on either side of active_page
var active_page_value = $('.active_page').attr('longdesc'); 

$('.page_link').each(function(){

    var longdesc = $(this).attr('longdesc');

    // This next part might be what's getting me...
    if( longdesc <= active_page_value+4 && longdesc >= active_page_value-4){

        $(this).css('display','block');

    }

});

我想说的是,如果任何具有 page_link 类的元素的 longdesc 属性值大于 active_page longdesc - 4 的总和的值,或者小于 active_page longdesc + 4 的总和

4

3 回答 3

2

您需要先解析数字,然后再添加它们。将改为附加这些值

if( (longdesc <= parseInt(active_page_value ,10) +4)
     &&( longdesc >= parseInt(active_page_value,10) -4))

JS

//Now, we need to paginate the pagination
var page_link_count = $('.page_link').length; 

//Hide all the .page_links
$('.page_link').css('display','none'); 

//Show active_page
$('.active_page').css('display','block'); 

//Show up to four .page_link on either side of active_page
var active_page_value = $('.active_page').attr('longdesc'); 

var min = parseInt(active_page_value,10) - 4;
var max = parseInt(active_page_value,10) + 4;

$('.page_link').each(function(){
    var longdesc = $(this).attr('longdesc');

    if( longdesc <= max && longdesc >= min){
        $(this).css('display','block');
    }

});​
于 2012-12-14T22:11:00.457 回答
0

有两点要改变:

改变

var page_link_count = $('.page_link').length();

var page_link_count = $('.page_link').length; 

var active_page_value = $('.active_page').attr('longdesc');

var active_page_value = +$('.active_page').attr('longdesc');
于 2012-12-14T22:10:52.860 回答
0

过于复杂的事情......这就是你所需要的。

//Hide all the .page_links
$('.page_link').hide(); 

//Show active_page
$('.active_page').show(); 

//Show all links before active
$('a.active_page').show();
$('a.active_page').prevAll().show();

*注意:最后两个可以合并,但我不想让你感到困惑。

于 2012-12-14T22:17:37.870 回答