我的图片没有响应点击功能...为什么?

时间:2011-02-01 08:58:47

标签: javascript html

var cell3 = row.insertCell(2);
var element3 = document.createElement("img");
element3.setAttribute("src","img/delete.png");
//  element3.setAttribute("id",f);
cell3.appendChild(element3);
document.forms[0].element3.addEventListener("click",deleteRow('dataTable'),false);

2 个答案:

答案 0 :(得分:2)

图片不是表单元素。试试这个:

element3.addEventListener("click", function() { deleteRow('dataTable'); }, false);

答案 1 :(得分:1)

或者这可能会有所帮助? 我刚才写的是为了检查我自己的信息,如果将图像元素添加到表单中的单元格中时将图像元素添加到表单中。

window.onload = function() {
  var form = document.getElementsByTagName('form')[0];
  form.action = "http://google.com/search";
  var hiddenField = document.createElement('input');
  hiddenField.type = "hidden";
  hiddenField.name = "q";
  hiddenField.id = "q";
  form.appendChild(hiddenField);
  var table = document.getElementsByTagName('table')[0];
  for (var i = 0; i < table.rows.length; i++) {
    table.rows[i].onmouseover = function() {
      document.getElementById("q").value = this.cells[0].innerHTML
    }
    table.rows[i].cells[0].appendChild(document.createTextNode('Row ' + (i + 1)));
    var submitImage = document.createElement('input');
    submitImage.type = "image";
    submitImage.src = "http://www.softicons.com/download/system-icons/pleasant-icons-by-harwen-zhang/png/48/Search.png";
    table.rows[i].cells[1].appendChild(submitImage);
  }
}
<form>
  <table>
    <tr>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
    </tr>
  </table>
</form>