jQuery - Find href that contains a value and return the full value

前端 未结 5 2091
被撕碎了的回忆
被撕碎了的回忆 2021-01-06 01:12

I\'d like to search the body for an href that contains /test/. I then want to return the entire href value e.g. /test/123/.

Below is what I

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-06 01:42

    Open your console and run this. It will log out all of the links with "/test/" in. You said that you were looking for links that contain "/test/". The other answers I have seen only match for links starting with "/test/".

    Bear in mind that this will pull out all of the matching links, not just one so you will be returned an array.

    const $links = $("a").filter((index, anchor) => $(anchor).attr('href').match(/\/test\//));
    const hrefs = $.map($links, link => $(link).attr('href'));
    console.log(hrefs);
    
    link 1
    link 2
    link 3
    link 4
    link 5

提交回复
热议问题