从JS节点中的模板操纵按钮属性

问题描述:

我有一个HTML模板,由包含三个按钮的DIV组成。从JS节点中的模板操纵按钮属性

<template ID="template3buttons"> 
    <div ID="container3buttons" class="boxes"> 
     <div ID= "sunShadeUp" class="boxes"> 
      <input type="button" 
       id="SunshadeButtonUp" 
       class="SunshadeSmallButton" 
       onclick="GenericAction('ENO_ShutterStop($all)', 'all')" 
       value=""/> 
     </div> 
     <div ID= "sunShadeStop" class="boxes"> 
      <input type="button" 
       id="SunshadeButtonStop" 
       class="SunshadeSmallButton" 
       onclick="GenericAction('ENO_ShutterStop($all)', 'all')" 
       value=""/> 
     </div> 
     <div ID= "sunShadeDown" class="boxes"> 
      <input type="button" 
       id="SunshadeButtonDown" 
       class="SunshadeSmallButton" 
       onclick="GenericAction('ENO_ShutterStop($all)', 'all')" 
       value=""/> 
     </div> 
    </div> 
</template> 

我使用JS生成模板的许多副本,最终将其插入适当的DIV中。

function create3Button(cellButtonId) //create boxes with 3 buttons each 
{ 
    var template3buttons = document.querySelector('#template3buttons'); 
    var insertionPoint= document.getElementById(cellButtonId); 
    var clone = document.importNode(template3buttons.content, true); 
    insertionPoint.appendChild(clone); 
} 

现在,我需要操作“onClick”标记为根据插入点的模板的每个副本的内容。我知道getElementById()不适用于DOM节点。有什么选择?

如果你使用jQuery,你可以这样做:

$("[id*=sunShade]").children().click(function() { 
    alert("Handler for .click() called."); 
}); 
+0

亲爱lkmac,感谢您的热心帮助。你的建议工作正常,如果我做 $(“#box11”)。children()。click(function(){ alert(“Handler for .click()called。”); }); 其中box11是包含模板副本的20个框之一。不过,我想知道是否有更优雅的方式从DIV的ID中提取数字“11”并以编程方式使用该数字,以便我不必保持20个连续的按钮。 – aag

+1

抱歉,最初没有得到您的答案。我更新了我的答案,现在就试试。 – luboskrnac

+0

完美 - 非常感谢! – aag