【发布时间】:2015-07-22 19:42:38
【问题描述】:
我正在构建一个包含 100 个 html 页面的简单游戏,当您浏览页面时,页面上会出现随机选择的故事片段。到目前为止,游戏运行良好,但我想修改它,以便我可以将包含故事的 javascript 数组放在每个 html 页面可以调用的单个文件中,这样我就可以编辑故事一次而不更新每次我添加 100 页。
最初我的 javascript 在“显示” div 内的每个页面上都是硬编码的。
编辑:我按照说明编辑了我的代码,但它仍然没有显示出来。
这是我的 html 页面:
<html>
<head>
<link rel="stylesheet" type="text/css" href="layout.css">
<link href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz' rel='stylesheet' type='text/css'>
<script type="text/javascript" src="story.js"></script>
</head>
<body>
<div class="header">
</div>
<div class="display_wrapper">
<div class="left_display">
</div>
<div class="display">
</div>
<div class="right_display">
</div>
</div>
<div class="left_nav">
</div>
<div class="nav">
<center>
<input type="button" id="north" value="north" onclick="window.location.href = +Math.floor(Math.random()
* 101) +'.html';" /> <br />
<input type="button" id="west" value="west" onclick="window.location.href = +Math.floor(Math.random() *
101) +'.html';" />
<input type="button" id="east" value="east" onclick="window.location.href = +Math.floor(Math.random() *
101) +'.html';" /> <br />
<input type="button" id="south" value="south" onclick="window.location.href = +Math.floor(Math.random()
* 101) +'.html';" />
</center>
</div>
<div class="right_nav">
</div>
</body>
</html>
这是我要调用的 javascript:
var r_text = new Array ();
r_text[0] = "Nothings here, you should keep moving before something shows up though.";
r_text[1] = "Nothings here, you should keep moving before something shows up though.";
r_text[2] = "Nothings here, you should keep moving before something shows up though.";
r_text[3] = "You found a piece of crumbled paper on the ground. It stood out on the ground because of the
sharp contrast between the white of the paper and the red of the blood staining it. Desperate for any
clues leading out of this eerie town, you pick it up and smooth it out carefully as to not damage it
further. It reads as follows: 888 paces north, 636 paces east, 770 paces west, 946 paces west.. no end in
sight..";
r_text[4] = "You can't see anything, you don't hear anything, but a chill just shot down your spine and you
cant shake this deep feeling of uneasiness.";
r_text[5] = "A strange smell like that of a rotting corpse hits your nose. You should turn back.";
r_text[6] = "You look and look and even shout out if anyone can hear you and find nothing, but you cant
shake the feeling that you're being watched.";
r_text[7] = "You look and look and even shout out if anyone can hear you and find nothing, but you cant
shake the feeling that you're being watched.";
r_text[8] = "You walk down a street of decrep old buildings that all seem uninhabited. No one is around,
not even stray animals. For as far as you can see in every direction is nothing but more old ruined
buildings.";
r_text[9] = "You walk down a street of decrep old buildings that all seem uninhabited. No one is around,
not even stray animals. For as far as you can see in every direction is nothing but more old ruined
buildings.";
r_text[10] = "You walk down a street of decrep old buildings that all seem uninhabited. No one is around,
not even stray animals. For as far as you can see in every direction is nothing but more old ruined
buildings.";
r_text[11] = "You happen upon a building with an open doorway. The building looks stable enough to
explore so you walk up to the entrance. Near the doorway, you find an old dusty sign with writing you cant
quite read on it. After dusting the sign-off, you find out this build used to be the town's library. You
enter optimistically thinking you'll find some clues on how to leave the acursed town, or what happened
to the inhabitants. <p>Carefully you explore the internal perimeter of the library to make sure there's
nothing inside waiting for you. Upon securing that there are no other living things in the library beside
yourself, you switch to your next goal: finding information. Searching for hours and hours through
hundreds of books taken randomly from just about every shelf you skim the contents of every book looking
for some scrap of useful information. From what you can gather the town's name is Innsmouth and it used
to be a thriving fishery and gold refinery but war and inbreeding dwindled the town's population to
nearly nothing. <p>Esoteric cults moved into the desolate town with promise of economic recovery through
prayer and converted most of the surviving population, though rumors were the conversions were mostly by
force. Articles of disappearances were the only other useful bits of information that could be called
recent but the last one was dated fifteen years ago. You slump down in your chair, sigh a great sigh,
then stretch your legs and stand up to go find the nearest window. It's almost dark. You decide to spend
the night in the library. During your final inspection of the place before going to sleep for anything
lurking, you find a book that seemed wholly out of place, it looked like an ancient tome, extradanarily
thick and fantastically decorated. The very look of it was sinister. <p>For a minute you debated whether
you should look through it, toss it out of the building or find a whole other building to sleep in for
the night. Ultimately you decide you came here for useful information and this is the only book in the
a whole library that looks promising. Without picking it up, you stand near enough to read the title.. The
Necronomicon. Hesitantly, you pick the book up as if it were a venomous snake ready to strike at any
moment. Carefully you lay it on a nearby table and proceed to open the front cover.<p> You spend the
entire night pouring over the ancient time until the light of day stings your over-strained dry eyes. The
the only way to maintain your own sanity after reading the contents of The Necronomicon you resign yourself
believe this was the Bible to the esoteric cults who took over Innsmouth and it is nothing more than
religious fanatic gobbilty-gook. The world in which you and every other person on this earth and the
world portrayed in The Necronomicon just CANT be the same one. You try and try to get some sleep but
every time you close your eyes one of the frightful monsters depicted in the Necronomicon came to the
front of your mind's eye. You decide it would be best to put away the tome and not touch it again, and
focus on resting up so you may try venturing out tomorrow morning to find a way out of this increasingly
frightening town.";
r_text[12] = "You feel like you've been walking for hours without finding anything useful, any people,
animals, or ways out of the town.";
r_text[13] = "you find an abandoned general store, through the windows it looks like there may be some
rations still inside. You try for the door but its baracaded by some fallen roof beams. After surveying
the area for anything lurking and confirming that you are alone within normal earshot, you take a large
a chunk of fallen brick and break open an entrance through a large front window. The sound of the glass
breaking was the first sound you've heard in uncountable hours. Your heart pounds and you're sure SOMETHING
lurking out there heard you and must be coming after you now. You're first instinct is to hide. <p>You
hide behind a counter and scope out your surroundings fervently listening for the slightest little sound.
The sound of your own heart pounding makes it hard to hear anything else. Every time you think it's ok to
move, you realize your frightened state of mind can make five minutes feel like hours, again and again
you stop yourself from moving. Hours pass by and you notice a distinct change in the sun's position from
the shadows sprawled out on the floor. Finally, you decide its safe to venture out in search of rations
after one more survey of the inside of the store. Your efforts paid off in enough water to satisfy your
thirst and some old canned goods easily opened with tools also found in the shop you gladly chanced and
wolfed down. You decide to hold up in this store until you're fully recovered and resolved to venture out
in the town again to find a way out.";
r_text[14] = "while trying to find a suitable shelter for the soon-coming night, you walk up to a
building with an open doorway and peer inside. instantly a chill creeps down your spine. a glimpse of a
dusty sign catches your gaze and you quickly dust it off to read it. It reads: The Esoteric Order of
Dagon. This is one of the churches of the cult that took over the town and quite possibly caused the
town's mass dissertion.<p> After that terrifying realization gave way to the ability to control your
limbs again, you decide to run and find a different shelter as far away from this building as you can. ";
r_text[15] = "while walking around the town you notice most of the land appears to be flat and the
numerous old delatidated buildings made it very hard to see much more than a few hundred feet down a
single street at a time. All of a sudden you spot the first elevation you've to see since you've arrived. You
race for it to try and gain a vantage point of any kind to figure out which direction to go. The
elevation turns out to be less than 10 feet high and gives almost no vantage point as your view is still
obstructed by all of the ancient tottering buildings. You need need to climb on top of a roof to gain any
vantage. ";
r_text[16] = "during your time exploring the town you have noticed most of the roads have been unmarked
or had their street signs torn down with no remnant of the name left to be read. That fact is what made
this street before you stand out from all the other identical streets you've been walking up and down
endlessly. An intact street sign that read the Main street. What luck! It doesn't matter which direction you
follow the street, Main should always lead to other streets that will allow you to LEAVE the town! <p>You
follow the Main street for as far as your legs can take you day in and day out, only stopping during the
night to take shelter in nearby abandoned buildings, and looking for food and water. Weeks passed by and
still, not a single change in the scenery could be found, no people, no animals, everything in bitter
unyielding silence. There seems to be no foreseeable end to this street.";
r_text[17] = "the sun is high in the sky and heading toward dusk. it happens much quicker than it should
and light becomes scarce in this town with no electricity after the sun starts to come back down. This is
about the time I start looking for a shelter to stay the night in, preferably something with drinkable
water inside as the search for food was given up long ago. After trying three other houses on this street
I find one with a door I can pry open. Upon entering the house I'm overcome with the stench of mold and
mildew, which means there must be water somewhere. I decide to investigate. <p>Very silently I roam around
the house with nothing but a few sunspots to light my way. It's almost impossible to distinguish what
each room's original purpose was, there was no discernable kitchen or bathroom to be made out in such
little light without blindly groping in the dark to maybe find something that resembles a sink, toilet,
tub or exposed pipes, which was not something I was quite ignorant enough to do. Upon entering the next
room I hear something squish under my foot and feel a dampness. After walking a few more steps I discover
the entire floor is covered in less than an inch of water. I would imagine its rainwater leaked through
the same holes in the walls and roof the sunspots are coming from, however, I've been in this town for a
while now and I've not once seen it rain. <p>I kneel down and cup my hands to scoop up some water. First I
feel the texture, is it slimy, sludgy, or sooty? Next, I smell it, is it tainted or fresh rainwater? It
felt stagnant, not dirty but not necessarily fresh. It had been here a while for certain. The smell was
what threw me for a loop, it was distinctively salty, like ocean water. <p> Nonetheless, I couldn't drink
it.
I was, however, curious as to how salty ocean water got into this house. But the quickly fading sunlight
was not enough to give a thorough investigation. On my way toward what I thought was the exit, I found a
staircase leading up I had not found earlier. The light seemed the be stronger upstairs, so I thought a
few minutes of sunlight would be worth a vantage point of the town from a second-story window, and maybe
perhaps an easy-enough climb to a roof. <p>The stairs seemed sturdy enough but they creaked rather loudly
under my feet. I unconsciously held my breath as I walked up the stairs and exhaled when I could actually
see the second floor of the house, it wasn't brightly lit by any means but enough sun poured in from old
windows and holes in the roof to light the second floor well enough for it to be easily explored. The
smell of mildrew was stronger up here as well and I soon found the bottom of my shoes to be soaked.
Everything up here was wet like a firehose had been taken to it. I instinctively head toward the most
brightly lit room, hoping for an open window. Peering in the doorway I find my open window!
<p>Unfortunately, the room is flooded with several inches of water and broken household objects litterly
the floor, poking up from the water like bones from a hastily-dug grave. I decided to check the other
rooms first. The second room I try has a closed door and it won't budge, it seems to be baracaded from the
other side. The next room is open but there's a large hole in the middle of the floor and it doesn't seem
to be stable enough to walk across to get to the nearest window. The first room is the best option.
-------------------------------------- ";
r_text[18] = "Nestled down in my shelter for the night, having made sure all was quiet and vacant before
attempting to fall asleep. Abruptly, in the dead of the night, I'm awakened by the croaking of frogs
outside. Up until this very moment I hadn't heard or seen so much as a living insect, let alone a frog.
This disturbed me greatly.";
r_text[19] = "saw a fishfrog creature";
r_text[20] = "saw a cultist group";
r_text[21] = "saw cthulhu";
r_text[22] = "hear noises";
r_text[23] = "chased by a cultist";
r_text[24] = "find cultist materials";
r_text[25] = "spot cultists or fishfrogs on devils reef";
r_text[26] = "chased by fishfrogs";
r_text[27] = "found an underground tunnel";
r_text[28] = "cthulhu dreams";
r_text[29] = "I hear strange spectral piping";
r_text[30] = "when i saw the people, or what i thought were people, i instantly shouted out to them to
get their attention. i wish i hadnt done that.";
r_text[31] = "a night of despair";
r_text[32] = "i thought it was a dream but it wasnt, how did I get here?";
r_text[33] = "going inside a church to investigate";
r_text[34] = "event 34";
r_text[35] = "event 35";
r_text[36] = "event 36";
r_text[37] = "event 37";
r_text[38] = "event 38";
r_text[39] = "event 39";
r_text[40] = "event 40";
r_text[41] = "event 41";
r_text[42] = "event 42";
r_text[43] = "event 43";
r_text[44] = "event 44";
r_text[45] = "event 45";
r_text[46] = "event 46";
r_text[47] = "event 47";
r_text[48] = "event 48";
r_text[49] = "event 49";
r_text[50] = "event 50";
var i = Math.floor(51*Math.random())
document.getElementsByClassName("display")[0].append( r_text[i] );
【问题讨论】:
-
顺便说一句,我强烈建议您不要维护 100 个单独的 HTML 文件。您可以让用户单击调用更改页面的 JavaScript 的按钮,而不是让用户单击指向新页面的链接。这将允许您只需要维护 1 个 HTML 文件,而不是几十个。
-
我按照大家的说法编辑了代码,但“显示”div 中仍然没有显示任何内容
-
另外,我还真没想到。一切都已经设置好了,更改我的按钮代码以触发对话框显示而不是转到页面会比找出我现在遇到的问题更困难吗?因为如果我这样做了,我可以将我的对话硬编码到已经工作的同一页面中。我想如果它足够简单,我可以接受任何一个答案。稍后我会因为多次编辑 100 页而自责,尽管哈哈
-
您没有任何代码写入显示 div。您的网络浏览器并不关心您的脚本标签在哪里;这对 JS 的执行方式没有影响。仅调用
document.write()只会将您的文本添加到文档的末尾。你会想做document.getElementsByClassName("display")[0].append( r_text[i] ); -
我刚试过,也没有用。 =/
标签: javascript html rel