当我从选择列表中选择一个选项时,网页内容将消失



我正在为学校做一个项目,该项目使用javascript动态创建一个选择列表,然后显示与列表中所选选项相关的网页内容。我有一个函数characterBox(),它创建选择列表并调用另一个函数sortLines()。sortLines()用于显示选择列表中所选字符的行,并隐藏所有其他字符的行。

我遇到的问题是,当我从列表中选择一个角色名称时,"场景"div中的所有内容都会消失。我是新手,不知道我的代码哪里出了问题。

我只能使用纯JavaScript,无法重新配置HTML文档。如果能帮我调试代码,我将不胜感激,因为我已经在这方面折腾了几个小时,无法解决问题。非常感谢。

以下是HTML文件的一个小片段:

<?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>

和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", characterBox, false);//calls createListBox() when page loads
var sourceDoc = document.getElementById("scene");
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;
}
function characterBox(object, option) {
var boxHead = document.getElementById("characterList");//references div with id characterList
boxHead.innerHTML = "<p>Show Only Lines By:</p>";//adds <p> element to characterList div
var cList = document.createElement("select");//creates <select> element
boxHead.appendChild(cList);//appends <select> element to characterList div
var characters = uniqueElemText("h3");//creates characters array that calls uniqueElemText() and applies all h3 elements
var showAll = document.createElement("option");//creates option element
showAll.text = "Show All Character Lines";// value of option element
showAll.value = 'ALL';
cList.add(showAll);//adds showAll as option of selection list
for (var i = 0; i < characters.length; i++) {
var options = document.createElement("option");
options.innerHTML = characters[i];
cList.appendChild(options);
addEvent(cList, "change", sortLines, false);
}
}

function sortLines(e) {
var selection = document.getElementById("characterList");
var character = this.selection;
var displayStatus = "";//stores display status to displayStatus var; value empty string, default status applied by browser
for (var n = document.getElementById("scene").firstChild; n != null; n = n.nextSibling) {
var nodeName = document.createElement("h3");
if (nodeName.innerHTML.indexOf("<h3>" + character + "</h3>") != -1) {
displayStatus = "none";
}
else
displayStatus = "";
}
document.getElementById("scene").innerHTML = displayStatus;
}

您似乎想要动态构建全新的页面。如果你这样做,你将无法选择另一个字符行或再次显示原始页面。你要做的是隐藏必要的行,而不是从页面中删除它们。

我会做以下事情:

第一个选项:

  • 选择字符
  • 查找所有字符的标题及其行
  • 从每个元素中删除带有display:none的attribyte样式
  • 对于所有其他字符标题及其行:
    • 为每个元素添加带有display:none的attribyte样式

结果应该类似于:

<h3 style='display:none;'>PROSPERO</h3>
<blockquote style='display:none;'><i>Enter PROSPERO in his magic robes, and ARIEL</i></blockquote>
<blockquote style='display:none;'>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>
...

第二种选择:

  • 使用规则display:none将样式添加到类隐藏的头部分,或者如果可能,将相同的规则添加到play.css
  • 选择字符
  • 查找所有字符的标题及其行
  • 从每个元素中删除名为hide的类
  • 对于所有其他字符标题及其行:
    • 为每个元素添加类隐藏

结果应该类似于:

<h3 class='hide'>PROSPERO</h3>
<blockquote class='hide'><i>Enter PROSPERO in his magic robes, and ARIEL</i></blockquote>
<blockquote class='hide'>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>
...

和新的css规则:

.hide {
display: none;
}

我将sortLines()函数的名称更改为showLines()

最新更新