0

在我的网站中,我在 iframe.each href 中构建了一个文本菜单作为 menu.html,我将其放入单独的 div 中。我希望根据父页面的标题,将更改其中一个 div 的类(例如:如果父标题是“home”,则上述 div 的类更改为“标记”)。我已经尝试了以下脚本,但仍然无法正常工作。怎么了?再次感谢!

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> </title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript">
switch (window.parent.document.title) {
case "hoempage":document.getElementById("nada1").className="marked"; break;
case "about": document.getElementById("nada2").className = "marked"; break;
        case "jokes": document.getElementById("nada3").className = "marked"; break;
        case "freewares": document.getElementById("nada4").className = "marked"; break;
        case "links": document.getElementById("nada5").className = "marked"; break;
        default: alert("god damn!"); break;
    }

</script>
</head>
<body dir="rtl">
<ul class="menu1">
<li><a href="Index.html" target="_parent"><div id="nada1">homepage</div></a></li>
<li><a href="About.html" target="_parent"><div id="nada2">about</div></a>  </li>
<li><a href="Jokes.html" target="_parent"><div id="nada3">jokes</div></a></li>
<li><a href="freewares.html" target="_parent"><div id="nada4">freewares</div></a>    </li>
<li> <a href="NetGames.html" target="_parent"><div id="nada5">links</div></a>    </li>
</ul>
<br /><br /><hr />
<div class="write_mail">if you want to rgister to site updates enter name + email:
<form action="sent.php" method="post" >
    <input type="text" name="subscr" /><br />email
    <input type="text" name="perosnal"/><br />name
    <br />
    <input type="submit" name="submit" onclick="alert('good job and thanks!');"/>    </form></div>

</body>
</html>

编辑:我尝试了解决方案,但仍然无法正常工作。这是父代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>homepage</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body dir="rtl">
<iframe src="menu.html" class="navibar" height="430px" width="50px" scrolling="no" style="border-style:none"></iframe>
<br />
<p class="About">built by zetta the beginner 2012<br /> 
</p>
</body>
</html>

menu.html 是我在消息开头更新的代码。再次抱歉骚扰:(

4

2 回答 2

0

您忘记break;在每个case. 看,只要 acase有效,将执行以下命令,直到被 a 停止break

switch(value){
   case c0: f0();
   case c1: f1();
   case c2: f2();
   default: f3();
}

现在如果value等于c1f1()将被执行。下一个标签 ( case c2)f2被调用,f2and也是如此f3。您需要停止执行这些案例:

switch(value){
   case c0: f0(); break;
   case c1: f1(); break;
   case c2: f2(); break;
   default: f3(); break; // doesn't really need a break since it is the last
}

虽然乍一看这似乎违反直觉,但这是遵循 DRY 概念的唯一方法:

switch(myletter){
   case 'A':
   case 'B':
   case 'C': alert("Hey, it's either A,B or C"); break;
   default: alert("Sorry, I don't know this letter yet :/");
}

有关更多信息,请参阅MDN:switch

于 2012-10-08T11:21:42.743 回答
0

嵌入 iframe 代码的页面如下:

      <html>
<head>
<title>main</title>
<style>
.marked{background:yellow}
</style>
</head>
<body>
<div id="class1">dddddddddddddddd</div><div id="class2">dddddddddddddddd</div>
<iframe src="iframe.htm"></iframe>
</body>
</html>

iframe 页面代码:

    <html>    
    <head>   
    <title>something</title>
    <script type="text/javascript">       
    switch (window.parent.document.title) {         
        case  "main": window.parent.document.getElementById("class1").className = "marked";break;
        case "About": window.parent.document.getElementById("class2").className = "marked";break;
        case "Interesting facts":             
    document.getElementById("class3").className =  "marked";break;
        default: alert("I failed, sorry! :(");   break; 
     }    
    </script>   
    </head>
    <body></body>  
    </html>
于 2012-10-08T11:40:35.713 回答