当函数(lect)被调用时,我必须动态地创建'a‘标记!在这个标记的onclick()事件上,我需要知道它的id。下面是更清楚地解释问题的代码
function lect(j) {
var mydiv = document.getElementById("cd" + j);
var count = 3;
for (var k = 1; k <= 3; k++) {
var aTag = document.createElement('a');
var inn = "analysis" + k;
var id = "link" + k;
var hr = "#";
aTag.setAttribute('id', id);
aTag.setAttribute('href', hr);
aTag.innerHTML = inn;
aTag.onclick = function (e)
{ // here i want to get the id of tag, so that it could be passed to
// the second html page
location.href = 'gallery-2.html?lectName=' + //name of id// ;
};
mydiv.appendChild(aTag);
}
}请帮帮我,我该怎么做!
发布于 2013-11-05 17:28:05
你可以:
aTag.onclick = function (e)
{
location.href = 'gallery-2.html?lectName=' + this.id;
};在onclick事件中,this将引用aTag对象。
https://stackoverflow.com/questions/19795010
复制相似问题