简体   繁体   English

在innerHTML中调用javascript函数

[英]Calling javascript function within innerHTML

I created an addRow function which allows me to add row to a table. 我创建了一个addRow函数,该函数允许我向表中添加行。 After i Add the row, I wanted to create an option to delete the new row. 添加行之后,我想创建一个选项来删除新行。 Is there a way to recycle my function? 有没有办法回收我的功能?

function addRow() {
 var table = document.getElementById('dataTable');
 var rowCount = table.rows.length;
 var newRow= table.insertRow((1));
 var c0 = newRow.insertCell
 c0.innerHTML="<div ><img src='include/images/cross.png' alt='delete row' onclick='deleteRow(rowCount)'/></div>";
  }

function deleteRow(row){
 var elem = document.getElementById('tr'+row);
 var old = (elem.parentNode).removeChild(elem);
 }

how about 怎么样

onclick='deleteRow(this)'

and have 并有

function deleteRow(img) {
  var thisRow = img.parentNode.parentNode.parentNode; // div..td..tr
  thisRow.parentNode.removeChild(thisRow);
}

mplungjan technically answered before me, but I was already making an example so here you go: mplungjan从技术上回答了我,但我已经在举一个例子,所以您可以进行以下操作:

function addRow() {
    var table = document.getElementById('dataTable');
    var rowCount = table.rows.length;
    var newRow= table.insertRow();
    var c0 = newRow.insertCell();
    c0.innerHTML="<div><img src='http://0.tqn.com/d/cats/1/0/c/i/3/iStock_AngryCat425x282.jpg' alt='delete row' onclick='imageClick(this)'/></div>";
}

// Function for deleting the parent row of a clicked image
window.imageClick = function(img) {
    img.parentNode.parentNode.removeChild(img.parentNode);
}

Live example: http://jsfiddle.net/HPww7/1/ 实时示例: http//jsfiddle.net/HPww7/1/

jQuery allows for a much cleaner, more elegant and more cross browser compatible solution: jQuery允许使用一种更清洁,更优雅,更兼容跨浏览器的解决方案:

function addRow() {
    var table = $('#dataTable');
    var newRow = $('<tr><td><div></div></td></tr>');

    // Create an image to place in the new row
    var image = $('<img>')
        .attr('src', 'http://0.tqn.com/d/cats/1/0/c/i/3/iStock_AngryCat425x282.jpg')
        .attr('alt', 'delete row')
        .on('click', function() {
            // Delete parent row on click
            $(this).parents('tr').remove();
        });

    // Append the image to the row and the row to the table
    image.appendTo(newRow);
    newRow.appendTo(table);
}

Here's a live example: http://jsfiddle.net/HPww7/2/ 这是一个实时示例: http : //jsfiddle.net/HPww7/2/

Possible solution,using a button instead of the image, and keeping a reference to the table for the deleteRow function: 可能的解决方案,使用按钮代替图像,并为deleteRow函数保留对表的引用:

<table id='dataTable'></table>
<button onclick='addRow()'>add</button>
<script>
var table = document.getElementById('dataTable');
function addRow() {
 var rowCount = table.rows.length;
 var newRow= table.insertRow(0);
 var c0 = newRow.insertCell();
 var button = document.createElement("button");
 button.innerHTML = "delete";
 button.row = newRow;
 button.addEventListener("click", function(e) {
  deleteRow(this.row);
 })
  c0.appendChild( button );
}

function deleteRow(row){
  table.firstChild.removeChild(row);
}
</script>

In the image you can do: 在图像中,您可以执行以下操作:

<img onclick="deleteRow(this);" ...>

Then: 然后:

function deleteRow(el) {
  var node = el.parentNode;
  while (node && node.tagName.toLowerCase() != 'tr') {
    node = node.parentNode;
  }
  if (node) node.parentNode.removeChild(node);
}

The above allows the image to be anywhere in the cell, it goes up to the first TR ancestor and deletes it. 上面的内容允许图像位于单元格中的任何位置,并上升到第一个TR祖先并将其删除。 If there isn't one, it does nothing. 如果没有,它什么也不做。 Using a hard coded string of parent nodes will throw an error if the structure changes. 如果结构发生更改,使用父节点的硬编码字符串将引发错误。

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

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