简体   繁体   English

JS阻止函数作为onclick事件运行

[英]JS prevent a function from running as onclick event

I would like to disable a certain function from running as an onclick event. 我想禁止某个功能作为onclick事件运行。

<a href="#" onclick="myfunc1();myfunc2();"></a>

Here, I would like to disable myfunc1, not myfunc2. 在这里,我想禁用myfunc1,而不是myfunc2。 Actually I want to disable myfunc1 from the whole page, but anyway this is the only thing that I need. 实际上,我想从整个页面禁用myfunc1,但是无论如何,这是我唯一需要的。

I have no control over the page and I am using userscript or other script injection tools to achieve this. 我无法控制页面,并且正在使用用户脚本或其他脚本注入工具来实现此目的。

What I've tried: 我试过的

  1. Redefining the function after the page has loaded: I've tried adding an event listener to an event DOMContentLoaded with function(){ myfunc1 = function(){}; 页面加载后重新定义功能:我尝试将事件监听器添加到事件DOMContentLoaded中,并带有function(){myfunc1 = function(){}; } This seems to be working, but in a fast computer with fast internet connection, sometimes it runs before the myfunc1 is defined (in an external js file that is synchronously loaded). }这似乎可行,但是在具有快速Internet连接的快速计算机中,有时它会在定义myfunc1之前运行(在同步加载的外部js文件中)。 Is there any way that I can guarantee that the function will be executed after myfunc1 is defined? 有什么方法可以保证在定义myfunc1之后执行该函数?

  2. Is there any way that I can 'hijack' the onclick event to remove myfunc1 by its name? 有什么方法可以“劫持” onclick事件以通过其名称删除myfunc1?

You should use event listeners, and then you would be able to remove one with removeEventListener . 您应该使用事件侦听器,然后可以使用removeEventListener删除事件侦听removeEventListener If you can't alter the HTML source you will need something dirty like 如果您无法更改HTML源代码,则将需要一些类似的内容

 function myfunc1() { console.log('myfunc1'); } function myfunc2() { console.log('myfunc2'); } var a = document.querySelector('a[onclick="myfunc1();myfunc2();"]'); a.setAttribute('onclick', 'myfunc2();'); 
 <a href="#" onclick="myfunc1();myfunc2();">Click me</a> 

Or maybe you prefer hijacking the function instead of the event handler: 或者,您可能更喜欢劫持函数而不是事件处理程序:

 function myfunc1() { console.log('myfunc1'); } function myfunc2() { console.log('myfunc2'); } var a = document.querySelector('a[onclick="myfunc1();myfunc2();"]'); var myfunc1_; a.parentNode.addEventListener('click', function(e) { // Hijack if(a.contains(e.target)) { myfunc1_ = window.myfunc1; window.myfunc1 = function(){}; } }, true); a.addEventListener('click', function(e) { // Restore window.myfunc1 = myfunc1_; myfunc1_ = undefined; }); 
 <a href="#" onclick="myfunc1();myfunc2();">Click me</a> 

Another way this could be done is using Jquery and setting the onlick propery on the anchor tag to null. 可以完成此操作的另一种方法是使用Jquery并将锚标记上的onlick属性设置为null。 Then you could attach a click function with just myfunc2() attached. 然后,您可以仅附加myfunc2()附加一个单击函数。

$(document).ready(function () {
  $("a").prop("onclick", null);
  $("a").click(function(){
        myfunc2();
  });
});
function myfunc1() {
  console.log('1');
}
function myfunc2() {
  console.log('2');
}

<a class="test" href="#" onclick="myfunc1();myfunc2();">Example</a>

You can see the codepen here - http://codepen.io/anon/pen/BLBYpO 您可以在此处查看codepen- http: //codepen.io/anon/pen/BLBYpO

Perhaps you are into jQuery. 也许您喜欢jQuery。

$(document).ready(function(){
    var $btn = $('button[onclick*="funcOne()"]');

    $btn.each(function(){
        var newBtnClickAttr;
        var $this = $(this);
        var btnClickAttr = $this.attr("onclick");

        newBtnClickAttr = btnClickAttr.replace(/funcOne\(\)\;/g, "");

        $this.attr("onclick", newBtnClickAttr);
    });
});

Where in the variable $btn gets all the button element with an onclick attribute that contains funcOne() . 在变量$btn ,所有按钮元素都带有包含funcOne()的onclick属性。

In your case, this would be the function you would like to remove on the attribute eg, myfunc1(); 在您的情况下,这就是您想在属性上删除的函数,例如myfunc1(); .

Now that you have selected all of the elements with that onclick function. 现在,您已经使用该onclick函数选择了所有元素。 Loop them and get there current attribute value and remove the function name by replacing it with an empty string. 循环它们并获得当前属性值,并通过将其替换为空字符串来删除函数名称。

Now that you have the value which does not contain the function name that you have replace, you can now update the onclick attribute value with the value of newBtnClickAttr . 现在,您拥有的值不包含您要替换的函数名,现在您可以使用newBtnClickAttr的值更新onclick属性值。

Check this Sample Fiddle 检查此样本小提琴

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

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