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

时间:2013-12-19 21:49:52

标签: javascript dhtml

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

我遇到的问题是,当我从列表中选择一个字符名称时,“scene”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;
}

1 个答案:

答案 0 :(得分:0)

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

我会做以下事情:

第一个选项:

  • 选择字符
  • 找到所有角色的标题及其行
  • 从每个元素
  • 中移除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()