将onClick事件添加到document.createElement(" th") [英] Add onClick event to document.createElement("th")

查看:1107
本文介绍了将onClick事件添加到document.createElement(" th")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 document.createElement(th)



<$ p $向表中动态添加列p> var newTH = document.createElement('th');

有没有办法设置 onClick 为此属性,以便用户可以通过单击标题删除列?任何帮助都会很棒。如果这是不可能的,是否可以放入一些东西

  newTH.innerHTML 

使其有效?

解决方案

  var newTH = document.createElement('th'); 
newTH.innerHTML ='你好,世界!';
newTH.onclick = function(){
this.parentElement.removeChild(this);
};

var table = document.getElementById('content');
table.appendChild(newTH);

工作示例: http://jsfiddle.net/23tBM/



你也可以隐藏 this.style.display ='无'


I am dynamically adding columns to a table by using document.createElement("th")

var newTH = document.createElement('th');

Is there a way to set an onClick attribute for this so a user can delete the column by clicking on the header? Any help would be great. If this is not possible, is it possible to put something in

newTH.innerHTML

to make it work?

解决方案

var newTH = document.createElement('th');
newTH.innerHTML = 'Hello, World!';
newTH.onclick = function () {
    this.parentElement.removeChild(this);
};

var table = document.getElementById('content');
table.appendChild(newTH);

Working example: http://jsfiddle.net/23tBM/

You can also just hide with this.style.display = 'none'.

这篇关于将onClick事件添加到document.createElement(&quot; th&quot;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆