使用脚本添加选项卡时强制换行

时间:2014-02-08 16:43:23

标签: javascript jquery html

有人创建了此脚本,以添加一些链接到我已安装的现有标签菜单

var fauxTab = new Array();

fauxTab[0] = new Array("History","http://%LIVESCORINGHOST%/%YEAR%/home/%LEAGUEID%?MODULE=MESSAGE3","_top");
fauxTab[1] = new Array("Scoreboard","http://%LIVESCORINGHOST%/%YEAR%/home/%LEAGUEID%?MODULE=MESSAGE7","_top");
fauxTab[2] = new Array("Forum","http://forums.thehuddle.com/index.php?/forum/156-the-empire/","_blank");


try {for (var x=0;x<fauxTab.length;x++) document.getElementById("homepagetabs").innerHTML+="<li onclick='window.open(\""+fauxTab[x][1]+"\",\""+fauxTab[x][2]+"\")'>"+fauxTab[x][0]+"</li>"} catch(er) {}

创建的HTML在li类之间没有换行符,所以看起来像这样

<li onclick="window.open(&quot;http://www27.myfantasyleague.com/2014/home/15356?MODULE=MESSAGE3&quot;,&quot;_top&quot;)">History</li><li onclick="window.open(&quot;http://www27.myfantasyleague.com/2014/home/15356?MODULE=MESSAGE7&quot;,&quot;_top&quot;)">Scoreboard</li><li onclick="window.open(&quot;http://forums.thehuddle.com/index.php?/forum/156-the-empire/&quot;,&quot;_blank&quot;)">Forum</li>

如何编辑脚本,以便每个li类都有换行符,HTML就像这样

<li onclick="window.open(&quot;http://www27.myfantasyleague.com/2014/home/15356?MODULE=MESSAGE3&quot;,&quot;_top&quot;)">History</li>
<li onclick="window.open(&quot;http://www27.myfantasyleague.com/2014/home/15356?MODULE=MESSAGE7&quot;,&quot;_top&quot;)">Scoreboard</li>
<li onclick="window.open(&quot;http://forums.thehuddle.com/index.php?/forum/156-the-empire/&quot;,&quot;_blank&quot;)">Forum</li>

我正在尝试在我的css中使用“justify”均匀分布标签,但由于这些添加标签在HTML中没有单独的行空间,因此无法正常工作。

3 个答案:

答案 0 :(得分:1)

添加换行符代码或br代码:

document.getElementById("homepagetabs").innerHTML+="<li onclick='window.open(\""+fauxTab[x][1]+"\",\""+fauxTab[x][2]+"\")'>
"+fauxTab[x][0]+"</li><br>"
//                    ^This

所以你的代码看起来像是:

var fauxTab = new Array();

fauxTab[0] = new Array("History","http://%LIVESCORINGHOST%/%YEAR%/home/%LEAGUEID%?MODULE=MESSAGE3","_top");
fauxTab[1] = new Array("Scoreboard","http://%LIVESCORINGHOST%/%YEAR%/home/%LEAGUEID%?MODULE=MESSAGE7","_top");
fauxTab[2] = new Array("Forum","http://forums.thehuddle.com/index.php?/forum/156-the-empire/","_blank");


try {for (var x=0;x<fauxTab.length;x++) document.getElementById("homepagetabs").innerHTML+="<li onclick='window.open(\""+fauxTab[x][1]+"\",\""+fauxTab[x][2]+"\")'>"+fauxTab[x][0]+"</li><br>"} catch(er) {}

或者您可以使用Javascript String换行符(\n,Opera上的Opera9和IE8将其转换为\r\n):

document.getElementById("homepagetabs").innerHTML+="<li onclick='window.open(\""+fauxTab[x][1]+"\",\""+fauxTab[x][2]+"\")'>
"+fauxTab[x][0]+"</li>\n"
//                    ^This

所以你的代码看起来像是:

var fauxTab = new Array();

fauxTab[0] = new Array("History","http://%LIVESCORINGHOST%/%YEAR%/home/%LEAGUEID%?MODULE=MESSAGE3","_top");
fauxTab[1] = new Array("Scoreboard","http://%LIVESCORINGHOST%/%YEAR%/home/%LEAGUEID%?MODULE=MESSAGE7","_top");
fauxTab[2] = new Array("Forum","http://forums.thehuddle.com/index.php?/forum/156-the-empire/","_blank");


try {for (var x=0;x<fauxTab.length;x++) document.getElementById("homepagetabs").innerHTML+="<li onclick='window.open(\""+fauxTab[x][1]+"\",\""+fauxTab[x][2]+"\")'>"+fauxTab[x][0]+"</li>\n"} catch(er) {}

答案 1 :(得分:0)

只需添加制动线<br>

</li><br>"} catch(er) {}

答案 2 :(得分:0)

您应该使用换行符\ n

+="<li onclick='window.open(\""+fauxTab[x][1]+"\",\""+fauxTab[x][2]+"\")'>"+fauxTab[x][0]+"</li>\n"