新窗口无法正确加载

时间:2017-01-26 01:51:30

标签: javascript html

我正在制作游戏并为其添加教程。我试着建立一个链接到"食谱"因为我称之为网站,但它每天都会过期。因此,为了解决这个问题,我开辟了一个新窗口,里面有内容。唯一的问题是它正确地工作。当我打开窗户时,它似乎重复了两次。在下面的代码示例中,我对其进行了格式化,以便比在一行中更容易阅读。谢谢你的帮助



function newWindow() {
	myWindow = window.open("", "myWindow", "width=900,height=500");
	myWindow.document.write("<!DOCTYPE html>
<html>
<head>
<title> RSS Earth Recipies</title>
<style>
body{
	background-color: #f4dc42;
}
</style>
</head>
<body>
<p>
<h3>Shop:</h3>
<br>Fire = 5 Wood<br>
 Lumber Jack = 10 Fire and consumes one fire and water per second<br>
 House = 50 Wood and 50 Fire<br>
 Furnace = 30 Wood and takes one Wood to output on fire<br>
Soldier x 10 = -1 Barracks, 10 water and food and consumes 1 food and water per second<br>
Barracks = 10 houses <br>
 Wells = 20 Wood and 5 Food and consumes 1 Food per second but outputs 2 Water per second <br>
 Hunter = 10 Wood and 5 Water and consumes 1 Water per second but outputs 2 Food per second<br>
Breeders = 1 house and consumes 1 Fire, Water, and Food per second<br>
<h3>In case you didn't know</h3>
<br><ul>
<li>Every 30 minutes barbarians will raid your civilization and every hour a nation will sweep your civilization.</li>
<li>The number of barbarians can list from 1 to 1000 but for the nation it can go from 1 to 1 Million</li>
<li>This versoin of the game's <b>DESIGN</b> was only made for the 1:1 student laptops that were distributed by onslow conty schools</li>
<li>If you press F12 then you will find various things in the inspector</li>
<li>Press the '/' key and help will pop up</li>
</ul>
</p>
<br>
<hr>
<h1>Tutorial:</h1>
<br>
<ol>
<li>To start things off The first thing you should do is collect wood. You can do this by either pressing the 'Collect Wood' button at the bottom of the webpage or by pressing the 'w' key</li>
<li>Collect at least 30 wood to continue</li>
<li>When you have this collect 30 of both Food and Water. You can do this by pressing the 'Collect Food' and 'Collect Water' buttons at the bottom of the webpage or by pressing the 'q' key for water and the 'o' key for food.</li><li>Now that you have this, we can begin 'automated production' as i like to call it. Quickly press the 'Build Well' and 'Train Hunter' buttons at the bottom of the webpage <b>ONCE</b>. Now your civilization is off to a good start!</li>
<li>When your feeling kind of lost again turn your wood into fire. Remember 5 wood will give you 1 fire.</li><li>So you have that now when you collect 5 fire just go ahead and press the 'Train Lumber Jack' at the bottom of the webpage. Remember: a Lumber Jack will take up 1 food and water per second</li><li>Next when your ready, make sure you have 30 wood and buy a furnace. You can do this by pressing the 'Make Furnace' button at the bottom of the webpage. This will output one fire per second and will destroy one wood per second</li><li>Moving on to houses and barracks. A house will do good for you if you want to get breeders but their also good if you want to train an army. Houses can be converted into barrack if you have the right amount. In order to make a house you have to have 50 wood and fire.</li>
<li>The only reason Barracks are in the game is for the soldiers. Cant have an army with out a storage unit can you? Anyway thats basically the only reasoning you have Barracks. When you buy soldiers you will destroy the amount of Barracks nessesary in order to get troops</li><li>The deal with troops is to give your town a defence against Barbarians, nations, continents, and soon other players! But having troops comes with a cost. The lowest amount of troops you can train at one time is 10 and being that each one accounts for taking in 1 food and water per second your going to need a lot of food and water production!</li>
<li>Now were near the end of the tutorial. This last segment will be about Breeders. Breeders are used for population. Population is shown just above the earth right under the season stats. The reason why these are so useful is because you can convert your current population to either Lumber Jacks or Soldiers. Now in order to convert Population into Soldiers you will need to have at least one Barrack in town. Also keep in mind that the amount that you convert is the amount you have to provide for... Resource wise.</li><h3>Thank you for reading the tutorial! Best of luck!</h3><hr><br><h4><i>Liam Sperry</i></ol></body></html>
}
&#13;
<a onclick="newWindow()">view recipies</a>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

你可以试试这种方式,让我知道它是否有效。

function newWindow() {
    myWindow = window.open("", "myWindow", "width=900,height=500");
    myWindow.document.write("<!DOCTYPE html><html><head><title> RSS Earth Recipies</title><style>body{  background-color: #f4dc42;}</style></head><body><p><h3>Shop:</h3><br>Fire = 5 Wood<br> Lumber Jack = 10 Fire and consumes one fire and water per second<br> House = 50 Wood and 50 Fire<br> Furnace = 30 Wood and takes one Wood to output on fire<br>Soldier x 10 = -1 Barracks, 10 water and food and consumes 1 food and water per second<br>Barracks = 10 houses <br> Wells = 20 Wood and 5 Food and consumes 1 Food per second but outputs 2 Water per second <br> Hunter = 10 Wood and 5 Water and consumes 1 Water per second but outputs 2 Food per second<br>Breeders = 1 house and consumes 1 Fire, Water, and Food per second<br><h3>In case you didn't know</h3><br><ul><li>Every 30 minutes barbarians will raid your civilization and every hour a nation will sweep your civilization.</li><li>The number of barbarians can list from 1 to 1000 but for the nation it can go from 1 to 1 Million</li><li>This versoin of the game's <b>DESIGN</b> was only made for the 1:1 student laptops that were distributed by onslow conty schools</li><li>If you press F12 then you will find various things in the inspector</li><li>Press the '/' key and help will pop up</li></ul></p><br><hr><h1>Tutorial:</h1><br><ol><li>To start things off The first thing you should do is collect wood. You can do this by either pressing the 'Collect Wood' button at the bottom of the webpage or by pressing the 'w' key</li><li>Collect at least 30 wood to continue</li><li>When you have this collect 30 of both Food and Water. You can do this by pressing the 'Collect Food' and 'Collect Water' buttons at the bottom of the webpage or by pressing the 'q' key for water and the 'o' key for food.</li><li>Now that you have this, we can begin 'automated production' as i like to call it. Quickly press the 'Build Well' and 'Train Hunter' buttons at the bottom of the webpage <b>ONCE</b>. Now your civilization is off to a good start!</li><li>When your feeling kind of lost again turn your wood into fire. Remember 5 wood will give you 1 fire.</li><li>So you have that now when you collect 5 fire just go ahead and press the 'Train Lumber Jack' at the bottom of the webpage. Remember: a Lumber Jack will take up 1 food and water per second</li><li>Next when your ready, make sure you have 30 wood and buy a furnace. You can do this by pressing the 'Make Furnace' button at the bottom of the webpage. This will output one fire per second and will destroy one wood per second</li><li>Moving on to houses and barracks. A house will do good for you if you want to get breeders but their also good if you want to train an army. Houses can be converted into barrack if you have the right amount. In order to make a house you have to have 50 wood and fire.</li><li>The only reason Barracks are in the game is for the soldiers. Cant have an army with out a storage unit can you? Anyway thats basically the only reasoning you have Barracks. When you buy soldiers you will destroy the amount of Barracks nessesary in order to get troops</li><li>The deal with troops is to give your town a defence against Barbarians, nations, continents, and soon other players! But having troops comes with a cost. The lowest amount of troops you can train at one time is 10 and being that each one accounts for taking in 1 food and water per second your going to need a lot of food and water production!</li><li>Now were near the end of the tutorial. This last segment will be about Breeders. Breeders are used for population. Population is shown just above the earth right under the season stats. The reason why these are so useful is because you can convert your current population to either Lumber Jacks or Soldiers. Now in order to convert Population into Soldiers you will need to have at least one Barrack in town. Also keep in mind that the amount that you convert is the amount you have to provide for... Resource wise.</li><h3>Thank you for reading the tutorial! Best of luck!</h3><hr><br><h4><i>Liam Sperry</i></ol></body></html>"
}

我只是删除空格并输入你使用的。它现在正在运作。你不能输入这样的字符串:

var string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque quis eros lorem. Quisque eu ultricies nunc, et fermentum dui. Integer quis elementum nunc. Nullam a tempus nibh. // just entered here
     Lorem ipsum dolor sit amet, consectetur adipiscing elit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aliquam rutrum tristique tincidunt. Fusce mollis scelerisque mauris, non porttitor est egestas sed. Curabitur elementum, augue nec finibus tincidunt, nunc leo fermentum sapien, quis lobortis enim nunc at justo."

如果您有这样的字符串,并且您想以这种方式使用它,您可以在字符串中使用+,如下所示:

var string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque quis eros lorem. Quisque eu ultricies nunc, et fermentum dui. Integer quis elementum nunc. Nullam a tempus nibh."+
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Aliquam rutrum tristique tincidunt. Fusce mollis scelerisque mauris, non porttitor est egestas sed. Curabitur elementum, augue nec finibus tincidunt, nunc leo fermentum sapien, quis lobortis enim nunc at justo.";

希望这会有所帮助:)

答案 1 :(得分:0)

使用innerHTML来覆盖HTML,而不是写入已经存在的任何内容。 http://codepen.io/anon/pen/dNVoRd