admin管理员组文章数量:1434363
Hi I am struck with this problem.
I need to create a table with Onclicklisteners dynamically. so i prefered this way.
function create_weekmenu(json) { var column_list=json.week_list; var menu_table=document.getElementById("weekmenu"); var row=document.createElement('tr'); for(var i=0;i<column_list.length;i++) { var cell=document.createElement('th'); var span_ele=document.createElement('span'); if(span_ele.addEventListener) { span_ele.addEventListener('click', toggletable(column_list[i]),true); } else if(span_ele.attachEvent) { // IE < 9 :( span_ele.attachEvent('onclick', toggletable(column_list[i])); } span_ele.appendChild(document.createTextNode(column_list[i])) cell.appendChild(span_ele); row.appendChild(cell); } menu_table.appendChild(row); }
The Resultant element Structure I am getting is
<table id="weekmenu">
<tr>
<th>
<span>week_one</span>
</th>
<th>
<span>week_two</span>
</th>
</tr>
</table>
But i need a Element Structure like this,
<table id="weekmenu">
<tr>
<th>
<span onclick="toggle(week_one)'>week_one</span>
</th>
<th>
<span onclick="toggle(week_two)'>week_two</span>
</th>
</tr>
</table>
Further to notice: I could see that the onclick listener is triggering while creating the element. but its not binding with the element permanently.
What would be the solution.
I prefered to construct DOM structure using appendChild() than by .innerHTML or document.write().
Hi I am struck with this problem.
I need to create a table with Onclicklisteners dynamically. so i prefered this way.
function create_weekmenu(json) { var column_list=json.week_list; var menu_table=document.getElementById("weekmenu"); var row=document.createElement('tr'); for(var i=0;i<column_list.length;i++) { var cell=document.createElement('th'); var span_ele=document.createElement('span'); if(span_ele.addEventListener) { span_ele.addEventListener('click', toggletable(column_list[i]),true); } else if(span_ele.attachEvent) { // IE < 9 :( span_ele.attachEvent('onclick', toggletable(column_list[i])); } span_ele.appendChild(document.createTextNode(column_list[i])) cell.appendChild(span_ele); row.appendChild(cell); } menu_table.appendChild(row); }
The Resultant element Structure I am getting is
<table id="weekmenu">
<tr>
<th>
<span>week_one</span>
</th>
<th>
<span>week_two</span>
</th>
</tr>
</table>
But i need a Element Structure like this,
<table id="weekmenu">
<tr>
<th>
<span onclick="toggle(week_one)'>week_one</span>
</th>
<th>
<span onclick="toggle(week_two)'>week_two</span>
</th>
</tr>
</table>
Further to notice: I could see that the onclick listener is triggering while creating the element. but its not binding with the element permanently.
What would be the solution.
I prefered to construct DOM structure using appendChild() than by .innerHTML or document.write().
Share Improve this question asked Jan 12, 2014 at 10:07 samsam 2,4862 gold badges22 silver badges29 bronze badges 1-
1
I'm not sure if I understand you right. But registering a
click
listener withaddEventListener
/attachEvent
would not create anonclick="toggle(week_one)'
on the element, why do you need it that way? – t.niese Commented Jan 12, 2014 at 10:18
2 Answers
Reset to default 3The problem is that you're calling the toggleTable function when you attach it. That's why it's being triggered when you create the element.
span_ele.addEventListener('click', toggletable(column_list[i]),true);
To avoid that it should be:
span_ele.addEventListener('click', toggletable, true);
But obviously that doesn't pass in the column to toggle so it's not ideal.
I would use something like:
function create_weekmenu(json)
{
var column_list=json.week_list;
var menu_table=document.getElementById("weekmenu");
var row=document.createElement('tr');
for(var i=0;i<column_list.length;i++)
{
var cell=document.createElement('th');
var span_ele=document.createElement('span');
if(span_ele.addEventListener)
{
span_ele.addEventListener('click', function(col) {
return function() {
toggletable(col);
}
}(column_list[i]),true);
}
else if(span_ele.attachEvent)
{ // IE < 9 :(
span_ele.attachEvent('onclick', function(col) {
return function() {
toggletable(col);
}
}(column_list[i]));
}
span_ele.appendChild(document.createTextNode(column_list[i]))
cell.appendChild(span_ele);
row.appendChild(cell);
}
menu_table.appendChild(row);
}
You need to make sure you attach a function to the event handler, not the result of a function.
function create_weekmenu(json) {
var column_list = json.week_list;
var menu_table = document.getElementById("weekmenu");
var row = document.createElement('tr');
for (var i = 0; i < column_list.length; i++) {
var cell = document.createElement('th');
var span_ele = document.createElement('span');
span_ele.setAttribute('onclick', 'toggletable(column_list[' + i + '])');
span_ele.appendChild(document.createTextNode(column_list[i]))
cell.appendChild(span_ele);
row.appendChild(cell);
}
menu_table.appendChild(row);
};
本文标签: htmlCreate Element With EventListener Dynamically in JavaScriptStack Overflow
版权声明:本文标题:html - Create Element With EventListener Dynamically in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745633062a2667389.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论