类上的addEventListener

时间:2019-02-16 14:21:22

标签: javascript addeventlistener

我需要使用parse_dates=True, index_col = [2,1,0],因为我有几个相同的按钮,等等。 我在一个wordpress循环上工作,该循环为每个新文章显示一个按钮,并且当我们单击该按钮时,必须在每个事件上显示一个注册表。 当我单击按钮时,我希望显示表单并隐藏按钮。 有人可以帮助我吗?

对不起,如果有错误,我是法国人。

import xml.etree.ElementTree as ET

tree = ET.parse('input.xml')
root = tree.getroot()

tree2 = ET.parse('output.xml')
root2 = tree2.getroot()

translations = []
rotations = []
scales = []
nodeids = []
shape_nodes1 = []
shape_nodes2 = []
shape_nodes3 = []

x = 0
y = 0
z = 0
a = 0

for s3birch in root.findall("./Scene/TransformGroup/TransformGroup[@name='birch_stage2']"):
    translation = s3birch.get('translation')
    translations.append(translation)
    rotation = s3birch.get('rotation')
    rotations.append(rotation)
    scale = s3birch.get('scale')
    scales.append(scale)
    nodeid = s3birch.get('nodeId')
    nodeids.append(nodeid)
for shape in root.findall("./Scene/TransformGroup/TransformGroup[@name='birch_stage2']/Shape[@shapeId='1']"):
    shape_nodeid1 = shape.get('nodeId')
    shape_nodes1.append(shape_nodeid1)
for shape2 in root.findall("./Scene/TransformGroup/TransformGroup[@name='birch_stage2']/Shape/Shape[@shapeId='2']"):
    shape_nodeid2 = shape2.get('nodeId')
    shape_nodes2.append(shape_nodeid2)
for shape3 in root.findall("./Scene/TransformGroup/TransformGroup[@name='birch_stage2']/Shape[@shapeId='3']"):
    shape_nodeid3 = shape3.get('nodeId')
    shape_nodes3.append(shape_nodeid3)

print 'Translations: ', translations
print 'Rotations: ', rotations
print 'Scales: ', scales
print 'NodeIds: ', nodeids
print 'Shape nodeIds: ', shape_nodes1
print 'Shape nodeids2: ', shape_nodes2
print 'Shape NodeIds3: ', shape_nodes3

for new_s3birch in root2.findall("./Scene/TransformGroup/TransformGroup[@name='birch_stage2']"):
    old_translation = new_s3birch.get('translation')
    old_rotation = new_s3birch.get('rotation')
    old_scale = new_s3birch.get('scale')
    old_nodeid = new_s3birch.get('nodeId')
    new_s3birch.set('translation', translations[x])
    if rotations[x] is not None:
        new_s3birch.set('rotation', rotations[x])
    elif rotations[x] is None
        new_s3birch.set('rotation', '')
    if scales[x] is not None:
        new_s3birch.set('scale', scales[x])
    elif scales[x] is None:
        new_s3birch.set('scale', '')
    new_nodeid = new_s3birch.set('nodeId', nodeids[x])
    x += 1
for new_shape in root2.findall("./Scene/TransformGroup/TransformGroup[@name='birch_stage2']/Shape[@shapeId='1']"):
    old_shape = new_shape.get('nodeId')
    new_shape.set('nodeId', shape_nodes1[y])
    y += 1
for new_shape2 in root2.findall("./Scene/TransformGroup/TransformGroup[@name='birch_stage2']/Shape/Shape[@shapeId='2']"):
    old_shape2 = new_shape2.get('nodeId')
    new_shape2.set('nodeId', shape_nodes2[z])
    z += 1
for new_shape3 in root2.findall("./Scene/TransformGroup/TransformGroup[@name='birch_stage2']/Shape[@shapeId='3']"):
    old_shape3 = new_shape3.get('nodeId')
    new_shape3.set('nodeId', shape_nodes3[a])
    a += 1

tree2.write('output.xml')

3 个答案:

答案 0 :(得分:0)

您需要在事件处理程序中使用this

var buttons = document.getElementsByClassName('btn_inscription');

var MyFunction = function() {
   this.closest('.formulaire').style.display = 'block';
   this.style.display ='none';
}

for (let i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener('click', MyFunction.bind(buttons[i]));
}

答案 1 :(得分:0)

您需要使用.bind(thisArg[, arg1[, arg2[, ...]]])才能传递当前的索引和元素():

var bouton = document.getElementsByClassName('btn_inscription');
var formulaire = document.getElementsByClassName('formulaire');

var MyFonction = function(idx, event) {
    formulaire[idx].style.display = 'block';
    this.style.display ='none';
}

for (var i = 0; i < bouton.length; i++) {
    bouton[i].addEventListener('click', MyFonction.bind(bouton[i], i));
    //                                            ^^^^^^^^^^^^^^^^^^^
}
.formulaire {
  display: none;
 }
<button class="btn_inscription">1</button>
<button class="btn_inscription">2</button>
<button class="btn_inscription">3</button>
<div class="formulaire">1</div>
<div class="formulaire">2</div>
<div class="formulaire">3</div>

答案 2 :(得分:0)

getElementsByClassName(以及.getElementsByTagName.getElementsByName)返回node list对象(类似数组的容器)。您无法与节点列表上的各个元素属性和方法进行交互,而必须使用容器中的对象。要在节点列表中的所有元素上设置事件处理程序,可以遍历容器并一次设置一个处理程序。

现在,getElementsByClassName返回一个“活动节点列表”,该列表每次与文档进行交互时都会重新扫描该文档,以确保您的容器具有一组最新的匹配元素。这可能会导致页面性能大幅下降,并且对活动节点列表的需求很少见。相反,请使用更现代的.querySelectorAll(),它允许您传入任何有效的CSS选择器并返回静态节点列表。

// Get all the desired elements into a node list
let elements = document.querySelectorAll(".test");

// Convert the node list into an Array so we can
// safely use Array methods with it
let elementsArray = Array.prototype.slice.call(elements);

// Loop over the array of elements
elementsArray.forEach(function(elem){
  // Assign an event handler
  elem.addEventListener("click", function(){
    console.log("You clicked me!");
    this.style.backgroundColor = "#ff0";
  });
});
<div class="test">Something</div>
<div>Something</div>
<div class="test">Something</div>
<div>Something</div>
<div class="test">Something</div>