I need to add a class external
to all the external hyperlink URLS. To achieve this I wrote the javascript
Array.from(document.querySelectorAll('a')).forEach(a => {
a.classList.add(location.hostname === a.hostname || !a.hostname.length ? 'internal' : 'external');
});
The issue is that the sub-domains are also treated as external and the respective <a>
tag is getting external
class added. How can I exclude the hyperlinks of sub-domains and also 2 other domains (abc.com and xyz.com) from having the external
class added?
而不是location.hostname === a.hostname
我会尝试location.hostname.idexOf(a.hostname) >= 0
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.