javascript - 我正在尝试从 xhtml 文件动态提取 <h3> 元素并将它们放入选择列表中

标签 javascript dynamic xhtml

我正在尝试动态提取 xhtml 文件中的所有 h3 元素并将它们放入选择列表中。为什么我的选择列表是空的?选择列表最终将指向选定的元素并显示嵌套在该元素中的内容。但现在我只需要知道为什么我的选项没有显示在选择列表中。感谢您的帮助!

为我提供了 uniqueElemText(elemname)。我不知道是否应该调用此函数来生成选择列表中的内容。

这是我处理动态 HTML 的第一周,我感觉完全迷失了!

这是 XHTML 文件:

?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- 
   New Perspectives on HTML, XHTML and DHTML 4th Edition
   Tutorial 16
   Case Problem 4

   The Tempest
   Author: Collin Klopstein
   Date: December 15, 2013  

   Filename:         tempest.htm
   Supporting files: bio_out.jpg, globe_out.jpg, plays.css, plays_out.jpg,
                     scene.js, son_out.jpg, strat_out.jpg
-->

<title>The Tempest, Act V, Scene 1</title>
<link href="plays.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="scene.js"></script>

</head>

<body>
<div id="linklist">
   <img src="plays_out.jpg"  alt="The Plays" />
   <img src="son_out.jpg"  alt="The Sonnets" />
   <img src="bio_out.jpg" alt="Biography" />
   <img src="globe_out.jpg" alt="The Globe" />
   <img src="strat_out.jpg" alt="Stratford" />
</div>
<div id="title"><img src="tempest.jpg" alt="The Tempest" /></div>
<div id="actList"><table><tr>
   <td>ACT I</td><td>ACT II</td><td>ACT III</td>
   <td>ACT IV</td><td>ACT V</td>
</tr></table></div>

<div id="characterList"></div>


<div id="sceneIntro">
<h2>Lines from Act V, Scene 1</h2>
</div>

<div id="scene">
<h3>PROSPERO</h3>
<blockquote><i>Enter PROSPERO in his magic robes, and ARIEL</i></blockquote>
<blockquote>Now does my project gather to a head:<br />
My charms crack not; my spirits obey; and time<br />
Goes upright with his carriage. How's the day?
</blockquote>

<h3>ARIEL</h3>
<blockquote>On the sixth hour; at which time, my lord,<br />
You said our work should cease.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>I did say so,<br />
When first I raised the tempest. Say, my spirit,<br/>
How fares the king and's followers?
</blockquote>

<h3>ARIEL</h3>
<blockquote>Confined together<br />
In the same fashion as you gave in charge,<br />
Just as you left them; all prisoners, sir,<br />
In the line-grove which weather-fends your cell;<br />
They cannot budge till your release. The king,<br />
His brother and yours, abide all three distracted<br />
And the remainder mourning over them,<br />
Brimful of sorrow and dismay; but chiefly<br />
Him that you term'd, sir, 'The good old lord Gonzalo;<br />
His tears run down his beard, like winter's drops<br />
From eaves of reeds. Your charm so strongly works 'em<br />
That if you now beheld them, your affections<br />
Would become tender.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>Dost thou think so, spirit?
</blockquote>

<h3>ARIEL</h3>
<blockquote>Mine would, sir, were I human.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>And mine shall.<br />
Hast thou, which art but air, a touch, a feeling<br />
Of their afflictions, and shall not myself,<br />
One of their kind, that relish all as sharply,<br />
Passion as they, be kindlier moved than thou art?<br />
Though with their high wrongs I am struck to the quick,<br />
Yet with my nobler reason 'gaitist my fury<br />
Do I take part: the rarer action is<br />
In virtue than in vengeance: they being penitent,<br />
The sole drift of my purpose doth extend<br />
Not a frown further. Go release them, Ariel:<br />
My charms I'll break, their senses I'll restore,<br />
And they shall be themselves.
</blockquote>

<h3>ARIEL</h3>
<blockquote>I'll fetch them, sir.</blockquote>

<blockquote><i>Exit</i></blockquote>

<h3>PROSPERO</h3>
<blockquote>Ye elves of hills, brooks, standing lakes and groves,<br />
And ye that on the sands with printless foot<br />
Do chase the ebbing Neptune and do fly him<br />
When he comes back; you demi-puppets that<br />
By moonshine do the green sour ringlets make,<br />
Whereof the ewe not bites, and you whose pastime<br />
Is to make midnight mushrooms, that rejoice<br />
To hear the solemn curfew; by whose aid,<br />
Weak masters though ye be, I have bedimm'd<br />
The noontide sun, call'd forth the mutinous winds,<br />
And 'twixt the green sea and the azured vault<br />
Set roaring war: to the dread rattling thunder<br />
Have I given fire and rifted Jove's stout oak<br />
With his own bolt; the strong-based promontory<br />
Have I made shake and by the spurs pluck'd up<br />
The pine and cedar: graves at my command<br />
Have waked their sleepers, oped, and let 'em forth<br />
By my so potent art. But this rough magic<br />
I here abjure, and, when I have required<br />
Some heavenly music, which even now I do,<br />
To work mine end upon their senses that<br />
This airy charm is for, I'll break my staff,<br />
Bury it certain fathoms in the earth,<br />
And deeper than did ever plummet sound<br />
I'll drown my book.
</blockquote>

<blockquote><i>Solemn music</i></blockquote>

<blockquote><i>Re-enter ARIEL before: then ALONSO, with a frantic gesture, attended by GONZALO; SEBASTIAN and ANTONIO in like manner, attended by ADRIAN and FRANCISCO they all enter the circle which PROSPERO had made, and there stand charmed; which PROSPERO observes.</i></blockquote>

<h3>PROSPERO</h3>
<blockquote>A solemn air and the best comforter<br />
To an unsettled fancy cure thy brains,<br />
Now useless, boil'd within thy skull! There stand,<br />
For you are spell-stopp'd.<br />
Holy Gonzalo, honourable man,<br />
Mine eyes, even sociable to the show of thine,<br />
Fall fellowly drops. The charm dissolves apace,<br />
And as the morning steals upon the night,<br />
Melting the darkness, so their rising senses<br />
Begin to chase the ignorant fumes that mantle<br />
Their clearer reason. O good Gonzalo,<br />
My true preserver, and a loyal sir<br />
To him you follow'st! I will pay thy graces<br />
Home both in word and deed. Most cruelly<br />
Didst thou, Alonso, use me and my daughter:<br />
Thy brother was a furtherer in the act.<br />
Thou art pinch'd fort now, Sebastian. Flesh and blood,<br />
You, brother mine, that entertain'd ambition,<br />
Expell'd remorse and nature; who, with Sebastian,<br />
Whose inward pinches therefore are most strong,<br />
Would here have kill'd your king; I do forgive thee,<br />
Unnatural though thou art. Their understanding<br />
Begins to swell, and the approaching tide<br />
Will shortly fill the reasonable shore<br />
That now lies foul and muddy. Not one of them<br />
That yet looks on me, or would know me Ariel,<br />
Fetch me the hat and rapier in my cell:<br />
I will discase me, and myself present<br />
As I was sometime Milan: quickly, spirit;<br />
Thou shalt ere long be free.
</blockquote>

<blockquote><i>ARIEL sings and helps to attire him</i></blockquote>

<h3>ARIEL</h3>
<blockquote>Where the bee sucks. there suck I:<br />
In a cowslip's bell I lie;<br />
There I couch when owls do cry.<br />
On the bat's back I do fly<br />
After summer merrily.<br />
Merrily, merrily shall I live now<br />
Under the blossom that hangs on the bough.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>Why, that's my dainty Ariel! I shall miss thee:<br />
But yet thou shalt have freedom: so, so, so.<br />
To the king's ship, invisible as thou art:<br />
There shalt thou find the mariners asleep<br />
Under the hatches; the master and the BOATSWAIN<br />
Being awake, enforce them to this place,<br />
And presently, I prithee.
</blockquote>

<h3>ARIEL</h3>
<blockquote>I drink the air before me, and return<br />
Or ere your pulse twice beat.
</blockquote>

<blockquote><i>Exit</i></blockquote>

<h3>GONZALO</h3>
<blockquote>All torment, trouble, wonder and amazement<br />
Inhabits here: some heavenly power guide us<br />
Out of this fearful country!
</blockquote>

<h3>PROSPERO</h3>
<blockquote>Behold, sir king,<br />
The wronged Duke of Milan, Prospero:<br />
For more assurance that a living prince<br />
Does now speak to thee, I embrace thy body;<br />
And to thee and thy company I bid<br />
A hearty welcome.
</blockquote>

<h3>ALONSO</h3>
<blockquote>Whether thou best he or no,<br />
Or some enchanted trifle to abuse me,<br />
As late I have been, I not know: thy pulse<br />
Beats as of flesh and blood; and, since I saw thee,<br />
The affliction of my mind amends, with which,<br />
I fear, a madness held me: this must crave,<br />
An if this be at all, a most strange story.<br />
Thy dukedom I resign and do entreat<br />
Thou pardon me my wrongs. But how should Prospero<br />
Be living and be here?
</blockquote>

<h3>PROSPERO</h3>
<blockquote>First, noble friend,<br />
Let me embrace thine age, whose honour cannot<br />
Be measured or confined.
</blockquote>

<h3>GONZALO</h3>
<blockquote>Whether this be<br />
Or be not, I'll not swear.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>You do yet taste<br />
Some subtilties o' the isle, that will not let you<br />
Believe things certain. Welcome, my friends all!<br />
But you, my brace of lords, were I so minded,<br />
I here could pluck his highness' frown upon you<br />
And justify you traitors: at this time<br />
I will tell no tales.
</blockquote>

<h3>SEBASTIAN</h3>
<blockquote>[Aside] The devil speaks in him.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>No.<br />
For you, most wicked sir, whom to call brother<br />
Would even infect my mouth, I do forgive<br />
Thy rankest fault; all of them; and require<br />
My dukedom of thee, which perforce, I know,<br />
Thou must restore.
</blockquote>

<h3>ALONSO</h3>
<blockquote>If thou be'st Prospero,<br />
Give us particulars of thy preservation;<br />
How thou hast met us here, who three hours since<br />
Were wreck'd upon this shore; where I have lost--<br />
How sharp the point of this remembrance is!--<br />
My dear son Ferdinand.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>I am woe for't, sir.
</blockquote>

<h3>ALONSO</h3>
<blockquote>Irreparable is the loss, and patience<br />
Says it is past her cure.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>I rather think<br />
You have not sought her help, of whose soft grace<br />
For the like loss I have her sovereign aid<br />
And rest myself content.
</blockquote>

<h3>ALONSO</h3>
<blockquote>You the like loss!
</blockquote>

<h3>PROSPERO</h3>
<blockquote>As great to me as late; and, supportable<br />
To make the dear loss, have I means much weaker<br />
Than you may call to comfort you, for I<br />
Have lost my daughter.
</blockquote>

<h3>ALONSO</h3>
<blockquote>A daughter?<br />
O heavens, that they were living both in Naples,<br />
The king and queen there! that they were, I wish<br />
Myself were mudded in that oozy bed<br />
Where my son lies. When did you lose your daughter?
</blockquote>

<h3>PROSPERO</h3>
<blockquote>In this last tempest. I perceive these lords<br />
At this encounter do so much admire<br />
That they devour their reason and scarce think<br />
Their eyes do offices of truth, their words<br />
Are natural breath: but, howsoe'er you have<br />
Been justled from your senses, know for certain<br />
That I am Prospero and that very duke<br />
Which was thrust forth of Milan, who most strangely<br />
Upon this shore, where you were wreck'd, was landed,<br />
To be the lord on't. No more yet of this;<br />
For 'tis a chronicle of day by day,<br />
Not a relation for a breakfast nor<br />
Befitting this first meeting. Welcome, sir;<br />
This cell's my court: here have I few attendants<br />
And subjects none abroad: pray you, look in.<br />
My dukedom since you have given me again,<br />
I will requite you with as good a thing;<br />
At least bring forth a wonder, to content ye<br />
As much as me my dukedom.<br /><br />
<i>Here PROSPERO discovers FERDINAND and MIRANDA playing at chess</i></blockquote>

<h3>MIRANDA</h3>
<blockquote>Sweet lord, you play me false.
</blockquote>

<h3>FERDINAND</h3>
<blockquote>No, my dear'st love,<br />
I would not for the world.
</blockquote>

<h3>MIRANDA</h3>
<blockquote>Yes, for a score of kingdoms you should wrangle,<br />
And I would call it, fair play.
</blockquote>

<h3>ALONSO</h3>
<blockquote>If this prove<br />
A vision of the Island, one dear son<br />
Shall I twice lose.
</blockquote>

<h3>SEBASTIAN</h3>
<blockquote>A most high miracle!
</blockquote>

<h3>FERDINAND</h3>
<blockquote>Though the seas threaten, they are merciful;<br />
I have cursed them without cause.<br />
<i>Kneels</i></blockquote>

<h3>ALONSO</h3>
<blockquote>Now all the blessings<br />
Of a glad father compass thee about!<br />
Arise, and say how thou camest here.
</blockquote>

<h3>MIRANDA</h3>
<blockquote>O, wonder!<br />
How many goodly creatures are there here!<br />
How beauteous mankind is! O brave new world,<br />
That has such people in't!
</blockquote>

<h3>PROSPERO</h3>
<blockquote>'Tis new to thee.
</blockquote>

<h3>ALONSO</h3>
<blockquote>What is this maid with whom thou wast at play?<br />
Your eld'st acquaintance cannot be three hours:<br />
Is she the goddess that hath sever'd us,<br />
And brought us thus together?
</blockquote>

<h3>FERDINAND</h3>
<blockquote>Sir, she is mortal;<br />
But by immortal Providence she's mine:<br />
I chose her when I could not ask my father<br />
For his advice, nor thought I had one. She<br />
Is daughter to this famous Duke of Milan,<br />
Of whom so often I have heard renown,<br />
But never saw before; of whom I have<br />
Received a second life; and second father<br />
This lady makes him to me.
</blockquote>

<h3>ALONSO</h3>
<blockquote>I am hers:<br />
But, O, how oddly will it sound that I<br />
Must ask my child forgiveness!
</blockquote>

<h3>PROSPERO</h3>
<blockquote>There, sir, stop:<br />
Let us not burthen our remembrance with<br />
A heaviness that's gone.
</blockquote>

<h3>GONZALO</h3>
<blockquote>I have inly wept,<br />
Or should have spoke ere this. Look down, you god,<br />
And on this couple drop a blessed crown!<br />
For it is you that have chalk'd forth the way<br />
Which brought us hither.
</blockquote>

<h3>ALONSO</h3>
<blockquote>I say, Amen, Gonzalo!
</blockquote>

<h3>GONZALO</h3>
<blockquote>Was Milan thrust from Milan, that his issue<br />
Should become kings of Naples? O, rejoice<br />
Beyond a common joy, and set it down<br />
With gold on lasting pillars: In one voyage<br />
Did Claribel her husband find at Tunis,<br />
And Ferdinand, her brother, found a wife<br />
Where he himself was lost, Prospero his dukedom<br />
In a poor isle and all of us ourselves<br />
When no man was his own.
</blockquote>

<h3>ALONSO</h3>
<blockquote>[To FERDINAND and MIRANDA] Give me your hands:<br />
Let grief and sorrow still embrace his heart<br />
That doth not wish you joy!
</blockquote>

<h3>GONZALO</h3>
<blockquote>Be it so! Amen!<br />

<i>Re-enter ARIEL, with the Master and BOATSWAIN amazedly following</i></blockquote>

<h3>GONZALO</h3>
<blockquote>O, look, sir, look, sir! here is more of us:<br />
I prophesied, if a gallows were on land,<br />
This fellow could not drown. Now, blasphemy,<br />
That swear'st grace o'erboard, not an oath on shore?<br />
Hast thou no mouth by land? What is the news?
</blockquote>

<h3>BOATSWAIN</h3>
<blockquote>The best news is, that we have safely found<br />
Our king and company; the next, our ship--<br />
Which, but three glasses since, we gave out split--<br />
Is tight and yare and bravely rigg'd as when<br />
We first put out to sea.
</blockquote>

<h3>ARIEL</h3>
<blockquote>[Aside to PROSPERO] Sir, all this service<br />
Have I done since I went.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>[Aside to ARIEL] My tricksy spirit!
</blockquote>

<h3>ALONSO</h3>
<blockquote>These are not natural events; they strengthen<br />
From strange to stranger. Say, how came you hither?
</blockquote>

<h3>BOATSWAIN</h3>
<blockquote>If I did think, sir, I were well awake,<br />
I'ld strive to tell you. We were dead of sleep,<br />
And--how we know not--all clapp'd under hatches;<br />
Where but even now with strange and several noises<br />
Of roaring, shrieking, howling, jingling chains,<br />
And more diversity of sounds, all horrible,<br />
We were awaked; straightway, at liberty;<br />
Where we, in all her trim, freshly beheld<br />
Our royal, good and gallant ship, our master<br />
Capering to eye her: on a trice, so please you,<br />
Even in a dream, were we divided from them<br />
And were brought moping hither.
</blockquote>

<h3>ARIEL</h3>
<blockquote>[Aside to PROSPERO] Was't well done?
</blockquote>

<h3>PROSPERO</h3>
<blockquote>[Aside to ARIEL] Bravely, my diligence. Thou shalt be free.
</blockquote>

<h3>ALONSO</h3>
<blockquote>This is as strange a maze as e'er men trod<br />
And there is in this business more than nature<br />
Was ever conduct of: some oracle<br />
Must rectify our knowledge.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>Sir, my liege,<br />
Do not infest your mind with beating on<br />
The strangeness of this business; at pick'd leisure<br />
Which shall be shortly, single I'll resolve you,<br />
Which to you shall seem probable, of every<br />
These happen'd accidents; till when, be cheerful<br />
And think of each thing well.<br />

<blockquote><i>Aside to ARIEL</i></blockquote>Come hither, spirit:<br />
Set Caliban and his companions free;<br />
Untie the spell.<br />

<blockquote><i>Exit ARIEL</i></blockquote>How fares my gracious sir?<br />
There are yet missing of your company<br />
Some few odd lads that you remember not.<br /><br />
<i>Re-enter ARIEL, driving in CALIBAN, STEPHANO and TRINCULO, in their stolen apparel</i></blockquote>

<h3>STEPHANO</h3>
<blockquote>Every man shift for all the rest, and<br />
let no man take care for himself; for all is<br />
but fortune. Coragio, bully-monster, coragio!
</blockquote>

<h3>TRINCULO</h3>
<blockquote>If these be true spies which I wear in my head,<br />
here's a goodly sight.
</blockquote>

<h3>CALIBAN</h3>
<blockquote>O Setebos, these be brave spirits indeed!<br />
How fine my master is! I am afraid<br />
He will chastise me.
</blockquote>

<h3>SEBASTIAN</h3>
<blockquote>Ha, ha!<br />
What things are these, my lord Antonio?<br />
Will money buy 'em?
</blockquote>

<h3>ANTONIO</h3>
<blockquote>Very like; one of them<br />
Is a plain fish, and, no doubt, marketable.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>Mark but the badges of these men, my lords,<br />
Then say if they be true. This mis-shapen knave,<br />
His mother was a witch, and one so strong<br />
That could control the moon, make flows and ebbs,<br />
And deal in her command without her power.<br />
These three have robb'd me; and this demi-devil--<br />
For he's a bastard one--had plotted with them<br />
To take my life. Two of these fellows you<br />
Must know and own; this thing of darkness!<br />
Acknowledge mine.
</blockquote>

<h3>CALIBAN</h3>
<blockquote>I shall be pinch'd to death.
</blockquote>

<h3>ALONSO</h3>
<blockquote>Is not this Stephano, my drunken butler?
</blockquote>

<h3>SEBASTIAN</h3>
<blockquote>He is drunk now: where had he wine?
</blockquote>

<h3>ALONSO</h3>
<blockquote>And Trinculo is reeling ripe: where should they<br />
Find this grand liquor that hath gilded 'em?<br />
How camest thou in this pickle?
</blockquote>

<h3>TRINCULO</h3>
<blockquote>I have been in such a pickle since I<br />
saw you last that, I fear me, will never out of<br />
my bones: I shall not fear fly-blowing.
</blockquote>

<h3>SEBASTIAN</h3>
<blockquote>Why, how now, Stephano!
</blockquote>

<h3>STEPHANO</h3>
<blockquote>O, touch me not; I am not Stephano, but a cramp.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>You'ld be king o' the isle, sirrah?
</blockquote>

<h3>STEPHANO</h3>
<blockquote>I should have been a sore one then.
</blockquote>

<h3>ALONSO</h3>
<blockquote>This is a strange thing as e'er I look'd on.<br /><br />
<i>Pointing to Caliban</i></blockquote>

<h3>PROSPERO</h3>
<blockquote>He is as disproportion'd in his manners<br />
As in his shape. Go, sirrah, to my cell;<br />
Take with you your companions; as you look<br />
To have my pardon, trim it handsomely.
</blockquote>

<h3>CALIBAN</h3>
<blockquote>Ay, that I will; and I'll be wise hereafter<br />
And seek for grace. What a thrice-double ass<br />
Was I, to take this drunkard for a god<br />
And worship this dull fool!
</blockquote>

<h3>PROSPERO</h3>
<blockquote>Go to; away!
</blockquote>

<h3>ALONSO</h3>
<blockquote>Hence, and bestow your luggage where you found it.
</blockquote>

<h3>SEBASTIAN</h3>
<blockquote>Or stole it, rather.
</blockquote>

<blockquote><i>Exeunt CALIBAN, STEPHANO, and TRINCULO</i></blockquote>

<h3>PROSPERO</h3>
<blockquote>Sir, I invite your highness and your train<br />
To my poor cell, where you shall take your rest<br />
For this one night; which, part of it, I'll waste<br />
With such discourse as, I not doubt, shall make it<br />
Go quick away; the story of my life<br />
And the particular accidents gone by<br />
Since I came to this isle: and in the morn<br />
I'll bring you to your ship and so to Naples,<br />
Where I have hope to see the nuptial<br />
Of these our dear-beloved solemnized;<br />
And thence retire me to my Milan, where<br />
Every third thought shall be my grave.
</blockquote>

<h3>ALONSO</h3>
<blockquote>I long<br />
To hear the story of your life, which must<br />
Take the ear strangely.
</blockquote>

<h3>PROSPERO</h3>
<blockquote>I'll deliver all;<br />
And promise you calm seas, auspicious gales<br />
And sail so expeditious that shall catch<br />
Your royal fleet far off.<br />

<blockquote><i>Aside to ARIEL</i></blockquote>My Ariel, chick,<br />
That is thy charge: then to the elements<br />
Be free, and fare thou well! Please you, draw near.<br />

<blockquote><i>Exeunt</i></blockquote>EPILOGUE<br />
SPOKEN BY PROSPERO<br />
Now my charms are all o'erthrown,<br />
And what strength I have's mine own,<br />
Which is most faint: now, 'tis true,<br />
I must be here confined by you,<br />
Or sent to Naples. Let me not,<br />
Since I have my dukedom got<br />
And pardon'd the deceiver, dwell<br />
In this bare island by your spell;<br />
But release me from my bands<br />
With the help of your good hands:<br />
Gentle breath of yours my sails<br />
Must fill, or else my project fails,<br />
Which was to please. Now I want<br />
Spirits to enforce, art to enchant,<br />
And my ending is despair,<br />
Unless I be relieved by prayer,<br />
Which pierces so that it assaults<br />
Mercy itself and frees all faults.<br />
As you from crimes would pardon'd be,<br />
Let your indulgence set me free.
</blockquote>
</div>

</body>
</html>

这是我的 JavaScript 文件:

/*
   New Perspectives on HTML, XHTML, and DHTML 4th Edition
   Tutorial 16
   Case Problem 4

   Author: Collin Klopstein  
   Date: December 15, 2013    

   Filename: scene.js


   Function List:
   uniqueElemText(elemName)
      Returns the unique content from HTML tags with the
      tag name elemName. The list is sorted in alphabetical
      ordered and returned as an array.

*/



function addEvent(object, evName, fnName, cap) {
   if (object.attachEvent)
       object.attachEvent("on" + evName, fnName);
   else if (object.addEventListener)
       object.addEventListener(evName, fnName, cap);
}

addEvent(window, "load", createListBox, false);//calls createListBox() when page loads

function uniqueElemText(elemName) {
   elems = document.getElementsByTagName(elemName);
   elemsArray = new Array();

   for (var i=0; i<elems.length; i++) elemsArray[i]=elems[i].innerHTML;  
   elemsArray.sort();
   for (i=0; i<elemsArray.length-1; i++) {
      if (elemsArray[i]==elemsArray[i+1]) {
         elemsArray.splice(i+1,1);
         i--;
      }
   }
   return elemsArray;
}

var characters = new Array ("h3");
var sourceDoc; // document on which the selection list is based

function createListBox() {
    var cList = document.getElementById("characterList");
    cList.innerHTML = "<p>Show Only Lines By:</p>";
    var cSelect = document.createElement("select");//creates selection list
    cList.appendChild(cSelect);//appends the selection list element to the cList element

    sourceDoc = document.getElementById("scene");

    createOptions(sourceDoc, cSelect);//generate selection list
}

function levelNum(node) {
    for (var i = 0; i < characters.length; i++) {
        if (node.nodeName == characters[i]) 
            return i;
    }//node is a character
    return -1;//if node is not a character
}

function createOptions(object, option) {
    for (var n = object.firstChild; n != null; n = n.nextSibling) {
    //loops through all of the nodes within object

        var nodeLevel = levelNum(n);
        if (nodeLevel != -1) {
        //node represents a character

            var selectOption = document.createElement("option");//creates "option" element
            selectOption.innerHTML = n.innerHTML;
            option.appendChild(selectOption);
        }
    }
}

最佳答案

好的,根据您其他非常相似的问题来看,答案一定是这是一个 HTML 文件而不是 XHTML 文件。 (文件扩展名是 .html,对吧?那么无论文件内容是什么样子,它都是 HTML。)
您展示的 Javascript 只能在 XHTML 环境中运行。

解决方案是,在 HTML 中,行 varcharacters = new Array("h3"); 应替换为 varcharacters = new Array("H3") ; 因为在 HTML DOM 中,元素名称以大写形式存储,即使标签在源代码中以小写形式编写。

所以,请不要说您正在使用 XHTML,而实际上您并未使用 XHTML。当我尝试时,我确实使用了 XHTML,所以这就是为什么它对我来说工作得很好。这个问题几天前就可以解决,然后就没有必要一遍又一遍地重复这个问题了!

关于javascript - 我正在尝试从 xhtml 文件动态提取 <h3> 元素并将它们放入选择列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20604248/

相关文章:

javascript - LeafLetJS快速入门指南显示灰色(401未经授权)

javascript - 使用 Node JS 将特定名称的多个表单输入保存到一个数组中(npm 表单)

php - 使用纯 JavaScript 获取 PHP 生成的 div 的高度

css - [CSS]div 中的动态图像大小和位置,百分比为百分比。 (显示屏尺寸)

c - 指向结构的指针数组的动态分配

css - 如何在团队环境中管理大型网站的CSS而不困惑?

iphone - 移动:html5 与 xhtml

javascript - 不同页面上的相同 cookie(cookie 路径)

javascript - 单击链接时隐藏内容

html - 在两列中显示 4 个 <li> 元素的一些问题