在Javascript中处理动态生成的元素

在Javascript中处理动态生成的元素,javascript,button,Javascript,Button,有没有办法处理加载页面时动态生成的元素 例如,我在下面有一个代码 function makeAFrame(index) { var padding=document.createElement('p'); var frameDiv=document.createElement('div'); frameDiv.id="frame"; frameDiv.className="frame"; var placeButton=document.createEl

有没有办法处理加载页面时动态生成的元素

例如,我在下面有一个代码

function makeAFrame(index)
{
    var padding=document.createElement('p');
    var frameDiv=document.createElement('div');
    frameDiv.id="frame";
    frameDiv.className="frame";

    var placeButton=document.createElement('input');
    placeButton.id="bButton"+index;
    placeButton.className="bButton";
    placeButton.value="aaa";
    placeButton.type="button";

    var buyButton=document.createElement('input');
    buyButton.id="aButton"+index;
    buyButton.className="aButton";
    buyButton.value="Buy It Now";
    buyButton.type="button";

    var table=document.createElement('table');
    table.setAttribute('cellpadding','3');
    table.setAttribute('width','100%');

    var col1=["a", "b", "c","d","e:", "f"];

    var row;
    var text;
    var cell;
    var i=0;

    for(i=0;i<6;i++)
    {
        row = table.insertRow(i);

        text = document.createTextNode(col1[i]);
        cell = row.insertCell(0);
        cell.setAttribute('align','right');
        cell.setAttribute('width','30%');
        cell.appendChild(text);

        text = document.createTextNode(hey[index][i]);
        cell = row.insertCell(1);
        cell.setAttribute('align','left');
        cell.appendChild(text);
    }

    for(i=6;i<8;i++)
    {
        row = table.insertRow(i);

        text = document.createTextNode("");
        cell = row.insertCell(0);
        cell.setAttribute('align','right');
        cell.setAttribute('width','30%');
        cell.appendChild(text);

        if(i==7)
        {
            cell = row.insertCell(1);
            cell.setAttribute('align','left');
            cell.appendChild(placeButton);
            cell.appendChild(buyButton);
            break;
        }
        else if(i==6)
        text = document.createTextNode(remaining);
        cell = row.insertCell(1);
        cell.setAttribute('align','left');
        cell.appendChild(text);
    }

    var body=document.getElementById('body');
    frameDiv.appendChild(table);
     body.appendChild(frameDiv);
     body.appendChild(padding);
}
函数makeAFrame(索引)
{
var padding=document.createElement('p');
var frameDiv=document.createElement('div');
frameDiv.id=“frame”;
frameDiv.className=“frame”;
var placeButton=document.createElement('input');
placeButton.id=“bButton”+索引;
placeButton.className=“bButton”;
placeButton.value=“aaa”;
placeButton.type=“button”;
var buyButton=document.createElement('input');
buyButton.id=“aButton”+索引;
buyButton.className=“aButton”;
buyButton.value=“立即购买”;
buyButton.type=“button”;
var table=document.createElement('table');
setAttribute('cellpadding','3');
table.setAttribute('width','100%”);
var col1=[“a”、“b”、“c”、“d”、“e:”、“f”];
var行;
var文本;
var细胞;
var i=0;

对于(i=0;i为什么不在创建按钮时向按钮添加onclick处理程序,只需添加

  placeButton.setAttribute("onclick", "showPopUp();");

然后你可以在showPopUp函数中对你的特定按钮做出反应。

任何js摆弄代码你的代码中什么是数组
hey
?你能提供一个工作示例,这样我们就不必在迷雾中点击吗?编辑这个:我在那里编辑了代码。数组不重要。你可以用任何字符串值替换它。我刚刚试过,但你没有每次元素创建时,r语句都会运行。我希望在元素创建之后再处理它们。您到底想在什么时候添加处理程序,然后出现什么问题?当页面加载时,它会在每个表中生成一些表和按钮。当所有组件都显示在页面中时,我希望单击页面上的一个按钮并使其显示出来打开一个输入框,以便我可以键入。然后,您应该使用window.onload=whenpageload;这将向DOM添加一个事件处理程序,在加载页面时将触发该事件处理程序。在whenpageload函数中,您创建了一个按钮,该按钮将打开输入框,您可以查看window.prompt()输入框OK的函数找到了它:处理程序似乎没有附加.onclick-its:placeButton.setAttribute(“onclick”,“showPopUp();”);