简体   繁体   English

根据href URL隐藏锚标记

[英]Hide Anchor tag based on href URL

I was wondering if there is a possibility to HIDE anchor tags that refer to a particular URL. 我想知道是否有可能隐藏指向特定URL的锚标记。 I know there is possible to hide based on id like this with JavaScript: 我知道可以使用JavaScript像这样隐藏id:

 document.getElementById('someID').style.display = 'none'; 
 <a href="#" id="someID" style="display: none">Check</a> 

But let's say I want to hide all anchor tags based on URL example: www.example.com 但是,假设我要根据网址示例隐藏所有锚定标记:www.example.com

<a href="www.example.com" id="someID" style="display: none">Check</a>
<a href="www.example2.com" id="someID" style="display: none">Check</a>

I want to hide the first anchor tag, not the second that refers to example2.com 我想隐藏第一个锚标签,而不是第二个引用example2.com的标签

Is this possible with pure JavaScript and not jQuery? 纯JavaScript而不是jQuery是否可行?

You can use document.querySelector to select bu attribute value like this.I have used no jquery the only javascript is used. 您可以使用document.querySelector这样选择bu属性值。我没有使用jquery,仅使用了JavaScript。

 document.querySelector("[href='www.example.com']").style.display = 'none'; 
 <a href="www.example.com" id="someID" style="display:block">Check</a> <a href="www.example2.com" id="someID" style="display:block">Check</a> 

Simply loop through all anchor elements and then check their href : 只需遍历所有锚元素,然后检查其href

 var anchors = document.getElementsByTagName('a'); for (var i = 0; i < anchors.length; i++) { if (anchors[i].href == 'https://example.com/') { anchors[i].style.display = 'none'; } } 
 <a href="https://example.com/" id="someID">Check</a> <a href="https://example2.com/" id="someID">Check</a> 

You can make condition 你可以创造条件

var url = document.getElementsByTagName('a');

if (url.href = "www.example.com")
{
url.style.display = none;
}

It is not exact code. 这不是确切的代码。 i provided you example .kindly try it and let me know. 我提供了示例。请尝试一下,让我知道。 It is for single . 这是单身。 if you have many tags then loop all those 如果你有很多标签,那么循环所有这些

You can use javascript to do the job. 您可以使用javascript来完成这项工作。 Use querySelector to get all the elements with same id . 使用querySelector获取具有相同id所有元素。 Then loop the ids and compare the href link value. 然后循环ids并比较href链接值。

<script>
var elements = document.querySelectorAll("[id='someID']");
for(var i = 0; i < elements.length; i++) {
  if (elements[i].getAttribute("href") === "www.example.com") {
    elements[i].style.display='none';
  }
}
</script>

Working fiddle link 工作小提琴链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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