当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - jQuery Selector Between two Attributes values

问题描述:

I have the following html.

<span end_timestamp="4520" timestamp="4150" id="1">Elemnt</span>

<span end_timestamp="6510" timestamp="4940" id="2">Anotherone I want</span>

<span end_timestamp="8910" timestamp="6610" id="3">another</span>

How can I select the element that's number 4199 is higher than attribute "timestamp" and lower than "end_timestamp"

Neither of these have worked. Maybe I should create a condition?

$( "[timestamp]:gt(4199 ),[end_timestamp]:lt(4199 )").addClass( "highlight" );

$( "[timestamp]:gt(4199 )[end_timestamp]:lt(4199 )").addClass( "highlight" );

网友答案:

The selectors :gt and :lt operate on indices in a collection, not on attribute values. For your case you will want to use filter:

$("[timestamp][end_timestamp]")
    .filter(function() {
        return this.getAttribute("timestamp") < 4199 
            && 4199 < this.getAttribute("end_timestamp");
    })
    .addClass("highlight");
网友答案:

Created a JSFiddle here: http://jsfiddle.net/xrnzc5qy/1/

var testTime = 4199;

function findTime(time) {
    $('span').each(function () {
        if (time > $(this).attr('timestamp') && time < $(this).attr('end_timestamp')) {
            //Highlight matching element
            $(this).css('color', 'red');
        }
    });
}

findTime(testTime);
分享给朋友:
您可能感兴趣的文章:
随机阅读: