简体   繁体   English

关闭内联onClick事件

[英]turning inline onClick event on off

I'm doing some validation where i need to turn off inline onClick event and then turn it on. 我正在做一些验证,需要关闭内联onClick事件然后再将其打开。

Jquery on off method does't work for inline onClick event. jQuery on off方法不适用于嵌入式onClick事件。 The line below removes the onClick event. 下面的行将删除onClick事件。 Before this I'm saving event into an array 在此之前,我将事件保存到数组中

$('.elements a').prop('onclick',null).on('click');

Saving events into array 将事件保存到数组

    var arr = [];
    var i = 0;
     $('.elements a').each(function(){
        arr[i++] = $(this).attr('onclick');
       });

How can I reassign these events in same order to each element. 如何将这些事件以相同的顺序重新分配给每个元素。 Hope it can be added using addEventListener ? 希望可以使用addEventListener添加它?

Or is there any other way to achieve the same ? 还是有其他方法可以实现相同目标?

Since you have to use inline events handlers, you can store removed attributes in array and then just reset them back: 由于必须使用内联事件处理程序,因此可以将已删除的属性存储在数组中,然后只需将它们重置回即可:

 var events = $('.elements a').map(function() { var onclick = $(this).attr('onclick'); $(this).removeAttr('onclick'); return onclick; }).get(); // ... Apply events later $('.reset').click(function() { $('.elements a').each(function(i) { $(this).attr('onclick', events[i]) }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="elements"> <a href="#" onclick="console.log(1)">Link 1</a> <a href="#" onclick="console.log(2)">Link 2</a> <a href="#" onclick="console.log(3)">Link 3</a> </div> <button class="reset">Reset events</button> 

Here is a Javscript solution: 这是一个Javscript解决方案:

  1. Iterate the elements 迭代元素
  2. Check if the element id exists in a predefined object 检查元素id存在于预定义的对象中
  3. If it exists, remove the key from the object and re-assign it to the onclick event 如果存在,请从对象中删除键,然后将其重新分配给onclick事件
  4. If it doesn't exist, remove the event handler and save it on the object with the element id as key 如果不存在,请删除事件处理程序,并将其保存在以元素id为键的对象上

Demo Fiddle: http://jsfiddle.net/abhitalks/op6nt25f/ 演示小提琴: http : //jsfiddle.net/abhitalks/op6nt25f/

Demo Snippet: 演示片段:

 var btn = document.getElementById('btn'), anchors = document.querySelectorAll('.elements a'), elems = {}; btn.addEventListener('click', toggleClick); function toggleClick() { [].forEach.call(anchors, function(anchor) { if (anchor.id in elems) { anchor.setAttribute('onclick', elems[anchor.id]); delete elems[anchor.id]; btn.innerText = 'Disable click event'; } else { elems[anchor.id] = anchor.getAttribute('onclick'); anchor.setAttribute('onclick', ''); btn.innerText = 'Enable click event'; } }); } function dojob(elem) { snippet.log(elem.innerText); } 
 <script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script> <ul class="elements"> <li><a id="a1" href="#" onclick="dojob(this)">One</a></li> <li><a id="a2" href="#" onclick="dojob(this)">Two</a></li> <li><a id="a3" href="#" onclick="dojob(this)">Three</a></li> <li><a id="a4" href="#" onclick="dojob(this)">Four</a></li> </ul> <button id="btn">Disable click event</button> 

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

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