简体   繁体   中英

How do I test if a HTML link element (a) containing a link href exists at DOM?

I'm trying to check if a link already exists by it href property in a HTML document before to add another one.

The code is just like:

if (!$('a[href*="#businessInfoMenu"]')){
    //add new link   
}

Does anyone made something like that?

Thanks in advance,

You can check the length of the selector to see if there were any matches:

if ($('a[href*="#businessInfoMenu"]').length == 0) {
    //add new link
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM