使用动态 URL 创建动态数量的按钮

Creating a dynamic number of buttons with a dynamic URL

我觉得这个很亲近。我只是无法生成在新选项卡中打开的按钮。我卡住了!

     function buildButton(url,i,size) {
            document.write("Building Button with URL= "+url+"<p>");

            var btn = document.createElement("BUTTON");
            btn.appendChild(document.createTextNode("PIDs "+i+" to "+(i+size)));
            btn.setAttribute("href",url);
            btn.setAttribute("target","_blank");
            document.body.appendChild(btn);


}  

<button> 标签更改为 <a> 标签

按钮没有 hreftarget 属性,<a> 标签有。代码应该是这样的:

function buildButton(url,i,size) {
  document.write("Building Button with URL= "+url+"<p>");

  var a = document.createElement("a");
  a.appendChild(document.createTextNode("PIDs "+i+" to "+(i+size)));
  a.setAttribute("href",url);
  a.setAttribute("target","_blank");
  document.body.appendChild(a);
}

buildButton('https://whosebug.com/', 1, 1);
<!DOCTYPE html>
<html>
<body>

</body>
</html>

解决方法:

function buildButton(url,i,size) {
    var btn = document.createElement("INPUT");
    btn.setAttribute("type","button");
    btn.setAttribute("onclick", "window.open('"+url+"')");
    btn.setAttribute("value","PIDs "+i+" to "+(i+size));
    btn.setAttribute("target","_blank");
    document.body.appendChild(btn);
    document.write("<p>");
} 

另一个基于此 HTML button opening link in new tab 的解决方案。看来你已经想通了

function buildButton(url,i,size) {
    document.write("Building Button with URL= "+url+"<p>");

    var btn = document.createElement("button");
    btn.appendChild(document.createTextNode("PIDs "+i+" to "+(i+size)));
    btn.setAttribute("onclick","window.open('" +url+"', '_blank')");

    document.body.appendChild(btn);
}  
buildButton("http://www.google.com","Thing", 44) ;