javascript - 删除节点的内联事件处理程序/属性

时间:2016-03-12 19:55:37

标签: javascript html html5

给出dom的HTML节点, 我需要删除所有内联事件处理程序/属性,例如:onclick,onmouseover,onmousedown等。

我知道:

document.getElementById("some_id").attributes

返回所有属性,我可以消除一些attriubtes,但是还有一些属性,如:id,class,name等。

如何分离这两种属性?

解决问题的完全不同的方法也是一种选择。

编辑:我试图仅删除内联事件, 我还需要"保存"在删除前的其他地方,所以克隆完全处置不是一个选项

3 个答案:

答案 0 :(得分:4)

在这里,您获得了所有element attributes,制作了一个数组,如果attributeon开头,请检入循环。
然后将object name/value inline event handler push array var el = document.getElementById("button1"); var listOfEvents=[]; var attributes = [].slice.call(el.attributes); for (i = 0; i < attributes.length; i++){ var att= attributes[i].name; if(att.indexOf("on")===0){ var eventHandlers={}; eventHandlers.attribute=attributes[i].name; eventHandlers.value=attributes[i].value; listOfEvents.push(eventHandlers); el.attributes.removeNamedItem(att); } } 加入var el = document.getElementById("button1"); var listOfEvents = []; var attributes = [].slice.call(el.attributes); for (i = 0; i < attributes.length; i++) { var att = attributes[i].name; if (att.indexOf("on") === 0) { console.log(att); var eventHandlers = {}; eventHandlers.attribute = attributes[i].name; eventHandlers.value = attributes[i].value; listOfEvents.push(eventHandlers); el.attributes.removeNamedItem(att); } } console.log(listOfEvents); /* logs [[object Object] { attribute: "onmousedown", value: "mouseDown();" }, [object Object] { attribute: "onmouseup", value: "mouseUp();" }, [object Object] { attribute: "onclick", value: "doSomething(this);" }] */,最后将其从节点中删除:< / p>

<div>
  <input id="button1" type="button" onmousedown="mouseDown();" onmouseup="mouseUp();" onclick="doSomething(this);" value="Click Me" />
</div>

检查下面的 代码段

// we need nunits bytes from the original free sized block
p->s.size -= nunits; 

// move the pointer size bytes forward 
p += p->s.size; 

// now we are at the block we want to return, set correct size.
p->s.size = nunits;
{% extends "form_div_layout.html.twig" %}

{%- block choice_widget_expanded -%}
    <div {{ block('widget_container_attributes') }}>
    {% set i=1 %}
    {%- for child in form %}
        <div class="radio-{{i}}">
            {{- form_widget(child) -}}
            {{- form_label(child, null, {translation_domain: choice_translation_domain}) -}}
        </div>
        {% set i=i+1 %}
    {% endfor -%}
    </div>
{%- endblock choice_widget_expanded -%}

答案 1 :(得分:0)

如果您可以命名要删除/检查的所有处理程序,那么这将适合您(它基本上搜索您给它的属性并删除/保存它们 - 添加奖金,您还可以删除一些其他属性你想要用同样的方法,只需将它们添加到列表中:

{{3}}

顺便说一句,IDK wtf我想发布为&#34;强制性代码&#34;当链接fiddleJS时......只需检查我在这里提取javascript的链接,但你缺少html

var dataStorage = {};


function someFunction(num) {
  alert(num);
};


function removeInlineHandlers(elementID) {
  //Define all the attributes you want to remove;
  var removeableAttributes = ["onclick", "onhover", "onmouseout"];
  var attributes = document.getElementById(elementID).attributes;
  var addFlag = true;
  var i = 0;
  var j = 0;

  for (i = 0; i < attributes.length; i++) {
    for (j = 0; j < removeableAttributes.length; j++) {
      if (attributes[i].name == removeableAttributes[j]) {
        // If this is the first attribute to be removed, add an object
        // to track the data of removeals.
        if (addFlag) {
          dataStorage[elementID] = {};
          addFlag = false;
        }

        dataStorage[elementID][attributes[i].name] = attributes[i].nodeValue;
        document.getElementById(elementID).removeAttribute(attributes[i].name);
        break;
      }
    }
  }
}

function removeHandlersAndPrint() {
  removeInlineHandlers("btn1");
  removeInlineHandlers("btn2");
  removeInlineHandlers("btn3");
  removeInlineHandlers("btn4");
  console.log(dataStorage);
}

答案 2 :(得分:0)

过滤所有名称以&#39;开头的属性。