【发布时间】:2015-03-08 05:57:35
【问题描述】:
我有一个由引用数组的 DOM 创建的按钮列表。单击列表中的按钮时,我想检索显示在按钮上的字符串。
我已尝试使用以下代码来引用字符串值,但未定义:
this.String;在函数内点击按钮时获取字符串。
如何正确检索字符串。
点击处理函数为:
$('.timeButtons').click(function() {
confirmation.push(this.textContent);
})
这就是按钮列表的创建方式:
var populateList=function(array){
var list = document.createElement('ul');
list.className="delete";
for(var i = 0; i < array.length;- i++) {
var item = document.createElement('li');
var itemButton=document.createElement('button');
itemButton.style.cssText='background:#f85a5a; border:none; width:200px; height:50px; margin-bottom:50px; align:center; border-radius:25px; color:#ffffff;'
itemButton.appendChild(document.createTextNode(array[i]));
item.appendChild(itemButton);
list.appendChild(item);
}
return list;
}
【问题讨论】:
-
你试过
this.textContent吗?请展示点击处理函数。 -
this.innerHTML 不起作用?
-
你在哪里设置
.timeButtons类? -
@dfsq 我将它设置在包含列表的 div 中。
标签: javascript dom button