创建按钮后,我是否可以添加链接或使用 window.location 方法,如下所示:`window.location = 'nextpage.html?foo=number'。我目前有这个
var typeValue = location.search;
var typeStringValue= typeValue.replace("?type=","");
var containers = typeValue.replace("?type=multi","");
var containersValue = parseInt(containers);
var sampleLetter = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
function createButton(buttonName){
var buttonDivBlock = document.getElementById("sampleSets");
var buttonElement = document.createElement("input");
buttonElement.setAttribute("type","button");
buttonElement.setAttribute("name",buttonName);
buttonElement.setAttribute("value","Sample Set"+" "+buttonName);
buttonElement.setAttribute("id",buttonName);
buttonDivBlock.appendChild(buttonElement);
// document.getElementById(sampleLetter[i]).setAttribute('onclick',window.location='SampleInfo.html'+typeStringValue+bottonName);<!--add the button link -->
}
function setButtons(numberOfContainers){
for(i=0;i<numberOfContainers;i++){
createButton(sampleLetter[i]);
}
}
window.onload = function(){
setButtons(containersValue);
}
但是document.getElementById("'"+sampleLetter[i]+"'").setAttribute('onclick',window.location='SampleInfo.html'+typeStringValue+bottonName);<!--add the button link -->
返回空值。
最佳答案
好吧,也许我可以举个例子来帮助你:
function getFive() { return 5;}
callOtherFunction("stringArgument", getFive());
callOtherFunction 的第二个参数将是 5,而不是 getFive 函数。在许多情况下,例如添加事件监听器和 AJAX 代码,您实际上希望将函数本身作为参数传递,以便稍后调用它。但如果您不想单独声明该函数,它看起来像这样:
callOtherFunction("stringArgument", function() { return 5; });
为了使代码看起来更简洁,您可以在 { 后面按 Enter 并创建多行函数。
现在,记住所有这些,再看看您注释掉的行。你看到缺少什么了吗? (PS。对“怂恿”格式表示歉意 - 我发现如果我帮助人们找到解决方案,而不是仅仅向他们展示解决方案,他们会更好地解决问题)
关于javascript - 如何添加动态创建按钮的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17623012/