2012-10-08 48 views
0

在我的網站中,我在iframe.each href內建立了一個文本菜單,作爲menu.html,我放入了單獨的div。我希望根據父頁面的標題,其中一個div的類將被更改(例如:如果父項的標題是「home」,則上述div的類將更改爲「已標記」)。我已經嘗試了下面的腳本,但它仍然沒有工作。哪裏不對?再次感謝!根據父標題在html頁面(位於iframe內)更改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是我在消息開始時更新的代碼。 騷擾:(

+0

是來自同一個域名的網頁? – mplungjan

+0

窗口不是一個對象。輸入錯誤?和案件沒有休息? – yukaizhao

+0

相同的域,有休息,它仍然拒絕工作。建議? –

回答

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

再對不起你忘了每個case後使用break;看,每當case是有效的,直到被break停止下面的命令將被執行。

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

現在,如果value 。等於c1f1()將要執行的下一個標籤(case c2),而不是f2被調用,所以是f2f3你需要停止的情況下執行:

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

+0

它仍然不執行類更改。如果標題是用希伯來文或阿拉伯文(不是英文),那麼這很重要嗎? –