:contain / start by

后端 未结 4 1379
無奈伤痛
無奈伤痛 2020-12-11 05:49

Is there a \"start by\" filter in jQuery, something like :contain but with a string beginning condition ?

相关标签:
4条回答
  • 2020-12-11 05:52
    Start With: ^=
    

    The ^= operator will filter elements whose attribute starts with the given value.

    More info here:

    http://www.bennadel.com/blog/1003-Cool-jQuery-Predicate-Selectors.htm

    jQuery Return elements where ID begins with a certain string

    0 讨论(0)
  • 2020-12-11 05:59

    Not that I know of, but you can easily implement your own selector for jQuery:

    $.extend($.expr[':'], {
        startsWith: function(elem,match) {  
            return (elem.textContent || elem.innerText || "").indexOf(match[3]) == 0;
        }  
    });
    

    Now you can use it like this:

    $("p:startsWith(Hello)").css("color","red")
    
    0 讨论(0)
  • No, but you can do it yourself using filter, as pulse suggested:

    $('a').filter(function(){
        return $(this).text().indexOf('start') == 0 }
    )
    

    You may want to use a regular expression here, to ignore case, or for more advanced searches:

    $('a').filter(function(){
        return $(this).text().match(/^start/i) }
    )
    
    0 讨论(0)
  • 2020-12-11 06:17

    I think you can use

    indexOf function in javascript

    var str = "test string print";
    
    str.indexOf ( "test" ) returns 0
    

    and

    str.indexOf ( "print" ) returns 12
    

    So you can check for the return value of indexOf function and if it is 0 then it is at the start position.

    0 讨论(0)
提交回复
热议问题