猿问

使用动态 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);



}


繁华开满天机
浏览 146回答 3
3回答

拉丁的传说

将<button>标签更改为<a>标签按钮没有href和target属性,<a>标签有。代码应该是这样的:function buildButton(url,i,size) {&nbsp; document.write("Building Button with URL= "+url+"<p>");&nbsp; var a = document.createElement("a");&nbsp; a.appendChild(document.createTextNode("PIDs "+i+" to "+(i+size)));&nbsp; a.setAttribute("href",url);&nbsp; a.setAttribute("target","_blank");&nbsp; document.body.appendChild(a);}buildButton('https://stackoverflow.com/', 1, 1);<!DOCTYPE html><html><body></body></html>

心有法竹

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

吃鸡游戏

另一个基于此HTML 按钮在新选项卡中打开链接的解决方案。看起来你已经想通了function buildButton(url,i,size) {&nbsp; &nbsp; document.write("Building Button with URL= "+url+"<p>");&nbsp; &nbsp; var btn = document.createElement("button");&nbsp; &nbsp; btn.appendChild(document.createTextNode("PIDs "+i+" to "+(i+size)));&nbsp; &nbsp; btn.setAttribute("onclick","window.open('" +url+"', '_blank')");&nbsp; &nbsp; document.body.appendChild(btn);}&nbsp;&nbsp;buildButton("http://www.google.com","Thing", 44) ;
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答