为什么javascript显示<br/> as astext

时间:2017-08-03 23:14:47

标签: javascript tags innerhtml

我有这个脚本:

&#13;
&#13;
<input type="number" id="userInput"=></input>
<button onclick="test()">Submit</button>
<p id="cmdpvp"></p>
<p id="cmdsafe"></p>
<p id="cmdscore"></p>
<script type="text/javascript">

function test(){
    var userInput = document.getElementById("userInput").value;
    var x = 20;
    var y = parseInt(userInput)
	var z = x + y;
    document.getElementById("cmdpvp").innerHTML = '<p><b>Command for PVPZone:</b></p><br><textarea rows="10" cols="100" id="html" name="html">/summon falling_block ~ ~1 ~ <br> {Block:command_block,Time:1,TileEntityData:<br>{Command:"/fill ~-1 ~-3 ~-2 ~3 ~-8 ~1 bedrock 0 hollow"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~0 ~-8 ~-1 minecraft:command_block 0 replace {Command:\"effect @a[r=' + z + ',score_PVPZone=1] minecraft:mining_fatigue 0 0\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~0 ~-8 ~-1 minecraft:command_block 0 replace {Command:\"effect @a[r=' + z + ',score_PVPZone=1] minecraft:regeneration 0 0\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~1 ~-11 ~0 minecraft:command_block 0 replace {Command:\"gamemode 0 @a[r=' + z + ',score_PVPZone=0]\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~1 ~-11 ~0 command_block 0 replace {Command:\"scoreboard players set @a[r=' + z + ',score_PVPZone=0] PVPZone 1\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~1 ~-11 ~0 command_block 0 replace {Command:\"scoreboard players set @a[r=' + z + ',score_SafeZone_min=1] SafeZone 0\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~0 ~-11 ~0 repeating_command_block 0 replace {auto:1b,Command:\"testfor @a[r=' + userInput + ',score_PVPZone=0]\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~1 ~-12 ~0 minecraft:unpowered_comparator 1"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~2 ~-13 ~0 command_block 0 replace {Command:\"fill ~-1 ~-1 ~-1 ~-1 ~-3 ~-1 minecraft:redstone_block\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~2 ~-17 ~-1 command_block 0 replace {Command:\"tellraw @a[r=' + z + ',score_PVPZone=0] [\\\"\\\",{\\\"text\\\":\\\"WARNING!!!!\\\",\\\"color\\\":\\\"red\\\"},{\\\"text\\\":\\\" You are now in the PVPZone!\\\"}]\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~2 ~-17 ~-1 command_block 0 replace {Command:\"fill ~-1 ~-1 ~ ~-1 ~1 ~ minecraft:air\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~2 ~-19 ~0 glowstone"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~0 ~-20 ~0 glowstone"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/fill ~ ~-14 ~-1 ~ ~2 ~ air"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/fill ~ ~-15 ~-1 ~ ~ ~-1 redstone_block"},Passengers:<br>[{id:falling_block,Block:redstone_block,Time:1<br>}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}</textarea><input type="button" value="Copy PVPZone" onclick="copy_to_clipboard(html);">';
	document.getElementById("cmdsafe").innerHTML = '<br><br><p><b>Command for SafeZone:</b></p><br><textarea rows="10" cols="100" id="test" name="htmltest">/summon falling_block ~ ~1 ~ <br>{Block:command_block,Time:1,TileEntityData:<br>{Command:"/fill ~-1 ~-8 ~-2 ~3 ~-3 ~1 bedrock 0 hollow"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~0 ~-8 ~0 command_block 0 replace {Command:\"scoreboard players set @a[r=' + z + ',score_PVPZone_min=1] PVPZone 0\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~0 ~-8 ~0 command_block 0 replace {Command:\"scoreboard players set @a[r=' + z + ',score_SafeZone=0] SafeZone 1\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~0 ~-8 ~0 command_block 0 replace {Command:\"gamemode 2 @a[r=' + z + ',score_SafeZone=1]\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~0 ~-8 ~0 repeating_command_block 0 replace {auto:1b,Command:\"testfor @a[r=' + userInput + ',score_SafeZone=0]\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~1 ~-12 ~0 repeating_command_block 0 replace {auto:1b,Command:\"effect @a[score_SafeZone_min=1] minecraft:weakness 2 255 true\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~1 ~-12 ~0 repeating_command_block 0 replace {auto:1b,Command:\"effect @a[score_SafeZone_min=1] minecraft:regeneration 2 255 true\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~1 ~-12 ~0 stone"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~1 ~-12 ~0 minecraft:unpowered_comparator 1"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~1 ~-16 ~-1 command_block 0 replace {Command:\"tellraw @a[r=' + z + ',score_SafeZone=0] [\\\"\\\",{\\\"text\\\":\\\"RELAX!!!!\\\",\\\"color\\\":\\\"green\\\"},{\\\"text\\\":\\\" You are now in the SafeZone!\\\"}]\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~1 ~-16 ~-1 command_block 0 replace {Command:\"fill ~-1 ~-1 ~ ~-1 ~1 ~ minecraft:air\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~1 ~-16 ~-1 repeating_command_block 0 replace {auto:1b,Command:\"effect @a[score_SafeZone_min=1] minecraft:mining_fatigue 2 255 true\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~2 ~-16 ~0 command_block 0 replace {Command:\"/fill ~-2 ~-1 ~-1 ~-2 ~-3 ~-1 minecraft:redstone_block\"}"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/setblock ~2 ~-19 ~0 glowstone"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/fill ~ ~-14 ~-1 ~ ~2 ~ air"},Passengers:<br>[{id:falling_block,Block:command_block,Time:1,TileEntityData:<br>{Command:"/fill ~ ~-15 ~-1 ~ ~ ~-1 redstone_block"},Passengers:<br>[{id:falling_block,Block:redstone_block,Time:1<br>}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}]}</textarea><input type="button" value="Copy Safezone" onclick="copy_to_clipboardtest(html);">';
	document.getElementById("cmdscore").innerHTML = '<br><br><p><b>Commands you have to paste in your chat:</b></p><br><textarea rows="1" cols="100" id="test" name="htmltest">/scoreboard objectives add SafeZone dummy</textarea><input type="button" value="Copy Command" onclick="copy_to_clipboardpvpscore(pvp);"><br><textarea rows="1" cols="100" id="test" name="htmltest">/scoreboard objectives add PVPZone dummy</textarea><input type="button" value="Copy Command" onclick="copy_to_clipboardsafescore(safe);">';

}
function copy_to_clipboard(html)
{
    document.getElementById('html').select();
    document.execCommand('copy');
}
function copy_to_clipboardtest(html)
{
    document.getElementById('test').select();
    document.execCommand('copy');
}
function copy_to_clipboardpvpscore(pvp)
{
    document.getElementById('test').select();
    document.execCommand('copy');
}
function copy_to_clipboardsafescore(safe)
{
    document.getElementById('test').select();
    document.execCommand('copy');
}
</script>
&#13;
&#13;
&#13;

javascript是一个html网页。输入一个数字,然后生成一个脚本。我知道它可能不是很聪明,而且方法更简单。当您单击提交按钮并出现一个脚本时,它会显示&lt; br&gt;作为文字。当它的内部HTML时,它不能工作吗?谁能解释一下?

1 个答案:

答案 0 :(得分:0)

一种方法是使用正则表达式来查找和替换所有br

以下是用新行替换它的那个:

.replace(/<br\s*\/?>/g, '\n')

如果您想创建自己的正则表达式并将其与“.replace”一起使用,Rubular是一个很好的资源!