简体   繁体   English

Javascript为动态表格行创建onclick事件

[英]Javascript creating an onclick event for a dynamic table row

I'm having trouble attaching an onclick event to my dynamic table rows. 我在将onclick事件附加到动态表行时遇到麻烦。

What I have is here: 我在这里:

function parseOrderLine(data) {
    var obj = JSON.parse(data);
    if (obj.length > 0) {
        var tbl = document.createElement('table');
        var row = tbl.insertRow(0);
        var cell = row.insertCell(0);
        cell.innerHTML = "Code";
        cell = row.insertCell(1);
        cell.innerHTML = "Size";
        cell = row.insertCell(2);
        cell.innerHTML = "Description";
        cell = row.insertCell(3);
        cell.innerHTML = "Type";
        cell = row.insertCell(4);
        cell.innerHTML = "Price";
        cell.style.textAlign = "right";
        for (var i = 0; i < obj.length; i++) {
            row = tbl.insertRow(i + 1);
            row.className = "results_row";
            cell = row.insertCell(0);
            cell.innerHTML = obj[i].Code;
            cell = row.insertCell(1);
            cell.innerHTML = obj[i].Size;
            cell = row.insertCell(2);
            cell.innerHTML = obj[i].Description;
            cell = row.insertCell(3);
            cell.innerHTML = obj[i].Type;
            cell = row.insertCell(4);
            cell.innerHTML = parseFloat(obj[i].Price).toFixed(2);
            cell.style.textAlign = "right";
            //row.addEventListener("click", function(){ alert('listen'); });
            row.onclick = (function(){ alert('click'); });
        }
        return tbl.outerHTML;
    }
    else { ...

As you can see I have tried both setting the onclick event and adding a listener. 如您所见,我已经尝试设置onclick事件和添加侦听器。 Neither seem to work. 似乎都不起作用。

I'm hoping someone can tell me I'm just being crazy and have overlooked something obvious. 我希望有人可以告诉我,我只是疯了,而忽略了一些明显的事情。 ;0) ; 0)

Any help appreciated guys. 任何帮助表示赞赏。

Thanks for reading. 谢谢阅读。

Jas

Your problem is on the tbl.outerHTML . 您的问题在tbl.outerHTML

When you add an event listener using addEventListener , Javascript does not add the method to your HTML code in the onclick attribute, it is added only to the DOM. 使用addEventListener添加事件侦听器时,Javascript不会将方法添加到onclick属性中的HTML代码中,而只会将其添加到DOM中。 So when you use tbl.outerHTML you listener isn't added to the returned HTML code. 因此,当您使用tbl.outerHTML您的侦听器不会添加到返回的HTML代码中。 The same is applicable for the row.onclick = function()... 这同样适用于row.onclick = function()...

If you want to keep the event listener when using outerHTML I suggest you go with setAttribute : 如果您想在使用outerHTML时保留事件监听器,建议您使用setAttribute

 function createTable() { var obj = [1,2,3,4,5]; var tbl = document.createElement('table'); var row = tbl.insertRow(0); var cell = row.insertCell(0); cell.innerHTML = "Code"; cell = row.insertCell(1); cell.innerHTML = "Size"; cell = row.insertCell(2); cell.innerHTML = "Description"; cell = row.insertCell(3); cell.innerHTML = "Type"; cell = row.insertCell(4); cell.innerHTML = "Price"; cell.style.textAlign = "right"; for (var i = 0; i < obj.length; i++) { row = tbl.insertRow(i + 1); row.className = "results_row"; cell = row.insertCell(0); cell.innerHTML = obj[i].Code; cell = row.insertCell(1); cell.innerHTML = obj[i].Size; cell = row.insertCell(2); cell.innerHTML = obj[i].Description; cell = row.insertCell(3); cell.innerHTML = obj[i].Type; cell = row.insertCell(4); cell.innerHTML = parseFloat(obj[i].Price).toFixed(2); cell.style.textAlign = "right"; //row.addEventListener("click", function(){ alert('listen'); }); row.setAttribute('onclick', "(function(){ alert('click'); })()"); } return tbl.outerHTML; } document.getElementById('test').innerHTML = createTable(); 
 <div id="test"></div> 

HOWEVER it's not the best solution, you should not use outerHTML when you want in fact use the object you just created (in your case, the table). 但是,这不是最佳解决方案,当您实际上要使用刚刚创建的对象(在您的情况下为表)时,不应使用outerHTML You should add it in the DOM using the appendChild method, doing this way you can use addEventListener : 您应该使用appendChild方法将其添加到DOM中,这样做可以使用addEventListener

 function createTable() { var obj = [1,2,3,4,5]; var tbl = document.createElement('table'); var row = tbl.insertRow(0); var cell = row.insertCell(0); cell.innerHTML = "Code"; cell = row.insertCell(1); cell.innerHTML = "Size"; cell = row.insertCell(2); cell.innerHTML = "Description"; cell = row.insertCell(3); cell.innerHTML = "Type"; cell = row.insertCell(4); cell.innerHTML = "Price"; cell.style.textAlign = "right"; for (var i = 0; i < obj.length; i++) { row = tbl.insertRow(i + 1); row.className = "results_row"; cell = row.insertCell(0); cell.innerHTML = obj[i].Code; cell = row.insertCell(1); cell.innerHTML = obj[i].Size; cell = row.insertCell(2); cell.innerHTML = obj[i].Description; cell = row.insertCell(3); cell.innerHTML = obj[i].Type; cell = row.insertCell(4); cell.innerHTML = parseFloat(obj[i].Price).toFixed(2); cell.style.textAlign = "right"; row.addEventListener("click", (function(){ alert('click'); })); } return tbl; } document.getElementById('test').appendChild(createTable()); 
 <div id="test"></div> 

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

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