简体   繁体   English

使用 JS 移除 onclick 功能

[英]Removing onclick funtion using JS

I am using a host that has a template for nav bars, I can style it but cannot edit the content within it.我正在使用具有导航栏模板的主机,我可以对其进行样式设置,但无法编辑其中的内容。 The search function brings up a search box, however I have developed an advanced search on the actual page /search.. Is there a way to remove the onclick and target from this link?搜索 function 会弹出一个搜索框,但是我在实际页面 /search 上开发了一个高级搜索。有没有办法从这个链接中删除 onclick 和目标? I cannot access the actual HTML for this to just remove it, therefore I have to have something override it.我无法访问实际的 HTML 来删除它,因此我必须有一些东西覆盖它。

 <a class="mainmenu" href="/search" onclick="showhide(document.getElementById('search_menu')); return false;" target="_blank">Search</a>

you can use removeAttribute function to remove those attributes.您可以使用removeAttribute function 删除这些属性。 below is a working example.下面是一个工作示例。

 window.onload=function(){ var anc=document.getElementsByClassName("mainmenu"); anc[0].removeAttribute("target"); anc[0].removeAttribute("onclick"); }
 <a class="mainmenu" href="/search" onclick="showhide(document.getElementById('search_menu')); return false;" target="_blank">Search</a>

Assigning null to the IDL attribute will clear the inline handler:将 null 分配给 IDL 属性将清除内联处理程序:

 const a = document.querySelector('.mainmenu'); a.onclick = null; a.removeAttribute('target');
 <a class="mainmenu" href="/search" onclick="showhide(document.getElementById('search_menu')); return false;" target="_blank">Search</a>

Remove the target and onclick attribute and replace the href attribute value with javascript:void(0) .删除targetonclick属性并将href属性值替换为javascript:void(0) You can use the below code for this.您可以为此使用以下代码。

 var a = document.getElementsByClassName('mainmenu')[0]; a.removeAttribute('onclick'); a.removeAttribute('target'); a.setAttribute('href', 'javascript:void(0)');
 <a class="mainmenu" href="/search" onclick="showhide(document.getElementById('search_menu')); return false;" target="_blank">Search</a>

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

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