简体   繁体   English

如何删除div标签下列出的href?

[英]How to remove a href that is listed under div tag?

I'm modifying a part of my wordpress theme and I have come across an issue. 我正在修改我的wordpress主题的一部分,但遇到了一个问题。 I want to remove the ability to click on a link though I do not have the ability to modify the class names or set ID tags 尽管我没有能力修改类名或设置ID标签,但我想取消单击链接的能力

HTML: HTML:

<div class="one">
  <a href="foobar.com">foobar.com</a>
</div>

I have tried to remove the functionality by writing this JavaScript: 我试图通过编写以下JavaScript来删除功能:

document.getElementsByClassName("one")
  .getElementsByTagName("a")
  .removeAttribute("href");

However this method does not work, am I doing something wrong? 但是此方法不起作用,我做错了吗?

You can use css. 您可以使用CSS。

.one a {
    pointer-events: none;
  }

and you can also remove the hand cursor by adding cursor: default; 您还可以通过添加cursor: default;来删除手形cursor: default;

 document.getElementsByTagName("a")[0].removeAttribute("href"); 
 <div class="one"> <a href="foobar.com">foobar.com</a> </div> 

No need to target .one first. 无需目标.one第一。 Just target by getElementsByTagName but remember that this returns an array of elements, so you need to reference the index of the element you want to remove. 只是通过getElementsByTagName目标,但是请记住,这将返回一个元素数组,因此您需要引用要删除的元素的索引。

document.getElementsByTagName("a")[0].removeAttribute("href");

如果只需要选择“一个”类中的链接,则需要索引从getElementsByClass返回的内容,然后调用子代并对其进行索引:

document.getElementsByClassName("one")[0].children[0].removeAttribute("href")

You can do it this way: 您可以这样操作:

  document.getElementById("text").onclick = function(){ var clickStatus = true; // Change this to true if you want to enable the link. if (clickStatus){ window.location = "http://foobar.com"; } } 
 <div class="one"> <section id="text">foobar.com</section> </div> 

It will generate Type Error because getElementsByClassName function will return a node list not a single node so you will need to do the following: 由于getElementsByClassName函数将返回一个节点列表而不是单个节点,因此将生成Type Error ,因此您需要执行以下操作:

document.getElementsByClassName("one")[0]
.getElementsByTagName("a")[0]
.removeAttribute("href");

or 要么

document.querySelector(".one a").removeAttribute("href");

Any method of the above will solve your problem 以上任何一种方法都可以解决您的问题

I hope it helps 希望对您有所帮助

I think you need to disable the Href functionalities here. 我认为您需要在此处禁用Href功能。 Please use Jquery : 请使用Jquery:

$('.one > a').on('click',function(e){    
  e.preventDefault();
}

Hope this will work for you 希望这对你有用

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

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