简体   繁体   English

如何为动态创建的按钮添加链接?

[英]How can I add a link to dynamically created button?

Once the buttons are created, is there anyway I can add a link or use window.location method like this: `window.location = 'nextpage.html?foo=number'. 一旦创建了按钮,无论如何我都可以添加一个链接或使用window.location方法,例如:'window.location ='nextpage.html?foo = number'。 I currently have this 我目前有这个

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);
}

But document.getElementById("'"+sampleLetter[i]+"'").setAttribute('onclick',window.location='SampleInfo.html'+typeStringValue+bottonName);<!--add the button link --> returns a null value. 但是document.getElementById("'"+sampleLetter[i]+"'").setAttribute('onclick',window.location='SampleInfo.html'+typeStringValue+bottonName);<!--add the button link -->返回空值。

Well, maybe I can help you along with an example: 好吧,也许我可以帮你举个例子:

function getFive() { return 5;}

callOtherFunction("stringArgument", getFive());

The second argument to callOtherFunction is going to be 5, not the getFive function. callOtherFunction的第二个参数将是5,而不是getFive函数。 In many cases, like adding event listeners and AJAX code, you actually want to pass the function itself as an argument, so it can be called later. 在许多情况下,例如添加事件侦听器和AJAX代码,您实际上希望将函数本身作为参数传递,以便以后可以调用。 But if you don't want to bother declaring that function seperately, it looks like this: 但是,如果您不想麻烦地单独声明该函数,则如下所示:

callOtherFunction("stringArgument", function() { return 5; });

To make code look cleaner, you can press Enter after the { and make a multi-line function. 为了使代码看起来更简洁,您可以在{之后按Enter键,然后创建多行功能。

Now, all that in mind, take another look at the line you've commented out. 现在,考虑到所有这些,请再次查看您注释掉的行。 Do you see what's missing? 你看到什么丢失了吗? (PS. Apologies for the "egging-on" format - I find people get much better at figuring things out if I help them find the solution, rather than just showing it to them) (PS。为“入门”格式而道歉-如果我帮助他们找到解决方案,而不是仅仅向他们展示,我发现人们会更擅长解决问题)

The sampleLetter variable is not defined where you are trying to use it (judging by commented code). 在尝试使用它的位置未定义sampleLetter变量(根据注释的代码判断)。 Use the value you had just set to the id attribute a few lines earlier. 使用您刚才在id属性中设置的值。

document.getElementById(buttonName).setAttribute( /* ... */ );

Or if you are trying to set it in the loop instead of in the createButton function, do not add the single quotes 或者,如果您尝试在循环中而不是在createButton函数中进行设置,请不要添加单引号

document.getElementById(sampleLetter[i]).setAttribute( /* ... */ );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM