2014-03-26 375 views
0

我正在嘗試創建能切換中文和英文網頁語言的Javascript。Javascript =來回切換網頁的語言?

我設置變量「中國」爲虛假文件打開時。然後我有一個if語句,說如果中文是假的,頁面將翻譯成中文(這個部分在我點擊時起作用)並使中文= true。然後還有另一個說法,如果中文是真的,頁面會翻譯回英文,並使中文= false。當我點擊它時,這部分不起作用。

$(document).ready(function() { 

var chinese=false 

if (chinese === false) { 
     $('#tochinese').click(function() { 
     $('#tochinese').html('<span>View in English</span>'); 
     $('#menu').html('菜單'); 
     $('#asynopsis').html('<a href="#synopsis" id="asynopsis">影片簡介</a>'); 
     $('#afilmmakers').html('<a href="#filmmakers" id="afilmmakers">主創簡介</a>'); 
     $('#aphotos').html('<a href="#photos" id="aphotos">幕後照片</a>'); 
     $('#acontact').html('<a href="#contact" id="acontact">聯繫諮詢</a>'); 
     $('#adonate').html('<a href="#donate" id="adonate">捐款合作</a>'); 
     $('#title span').html('<a href="/">殺死那逃羊</a>'); 
     $('#synopsis div').html(
      '<h1>影片簡介</h1>\ 
      <p>劉晨的母親楊慧嫺和自己清貧的丈夫離婚,帶着劉晨和擁有一個女兒胡茜,但因意外無法再生育兒子的企業老闆胡科良再婚,開始了新生活。胡科良重男輕女,無比珍惜自己的繼子,冷落了胡茜,讓胡茜心生不滿。另一方面,劉晨始終認定楊慧嫺爲了富貴生活狠心拋棄自己的生父,因此處處與楊慧嫺和自己的新家庭爲敵。</p>\ 
      <p>劉晨即將18歲成年。胡科良計劃給劉晨一個盛大的派對,但是劉晨並不領情,並執意要和自己的生父一起慶祝。回到家中,胡科良惱羞成怒,遷怒於楊慧嫺。劉晨聽着胡科良對楊慧嫺的斥責和打罵,幻想了自己衝進房間與胡科良殊死搏鬥的情景。</p>\ 
      <p>胡科良威脅楊慧嫺如果劉晨不願意接受自己,那麼,她以後便再也沒有和他一同出入上流社會的機會。楊慧嫺試圖說服劉晨,但劉晨痛斥楊慧嫺爲了自己榮華富貴而背叛生父,利用自己的自私本性。</p>\ 
      <p>隔天,胡科良丟下楊慧嫺獨自外出。呆在家中的楊慧嫺爲了討好胡科良,決定在家裏大掃除。劉晨和胡茜都不願意幫忙。胡茜在和寵物狗玩耍的時候,不小心破壞了胡科良的重要資料。晚上,胡科良回來之時,劉晨和楊慧嫺意外發現胡科良竟有外遇。另一方面,胡科良發現資料被破壞後大發雷霆,胡茜嫁禍給楊慧嫺並辱罵她。此時繼祖母陳芬主動開口承擔下了罪名。</p>\ 
      <p> 劉晨來到楊慧嫺的房間質問胡茜爲何嫁禍。兩人發生爭執,胡茜滿不在乎地謾罵劉晨和楊慧嫺。劉晨將胡茜撲倒在牀上企圖侮辱她。但是當一向高傲的胡茜沒了氣焰開始苦苦求饒的時候,劉晨突然失去了性趣。這時,劉晨聽到胡科良朝胡茜房間走來,腦海中幻想了當着胡科良的面蹂躪胡茜的畫面。回過神來,劉晨開門離去。</p>\ 
      <p>劉晨決定獨自離家出走去找自己的生父,直到過完生日爲止。當他拎着包悄悄下樓時,看見胡科良背對着自己看着報紙,而楊慧嫺正在一旁沖泡茶葉。劉晨靜靜地看着楊慧嫺取出一包粉末,倒入了茶杯中。楊慧嫺在轉身之時看到了劉晨,卻鎮定地把茶杯交給了胡科良。兩人看着胡科良將茶一飲而盡。楊慧嫺冷靜地朝劉晨走來,劉晨微微顫抖着轉過身,慢慢走上了樓梯。</p>\ 
      <p>房間裏,劉晨獨自坐在牀上發呆。楊慧嫺輕輕開門而入,從牀底下拿出行李箱開始整理行李。兩人一動一靜,保持着沉默。整個家裏一片安靜:胡茜蜷縮在牀上,桌上的音響緩緩播放着音樂;陳芬坐在房間的椅子上安靜地睡着,手裏的書本落在了地上;客廳裏,胡科良的背靠在沙發上,整個人一動不動。</p>\ 
      <p>屋外,整座房子安靜地佇立着,房間裏的燈光在夜色中顯得越來越小。</p>\ 
<p>-完-</p>'); 

     chinese=true 
     }); 
}; 

if (chinese===true) { 
     $('#tochinese').click(function() { 
     $('#menu').html('Menu') 
     $('#tochinese').html('<span>切換到英文</span>'); 
     $('#asynopsis').html('<a href="#synopsis" id="asynopsis">Synopsis</a>'); 
     $('#afilmmakers').html('<a href="#filmmakers" id="afilmmakers">Filmmakers</a>'); 
     $('#aphotos').html('<a href="#photos" id="aphotos">Photos</a>'); 
     $('#acontact').html('<a href="#contact" id="acontact">Contact</a>');  
     $('#adonate').html('<a href="#donate" id="adonate">Donate</a>'); 
     $('#title span').html('<a href="/">Raven Over the Edge</a>'); 
     $('#synopsis div').html(
      '<p>Liu’s mother Yang divorced her poor husband and married a wealthy entrepreneur Hu. Hu has a daughter Qian, but he is no longer able to have children after an accident. Qian is upset because Hu favors boys over girls, and Hu cares more about Liu than he does for Qian. On the other hand, Liu still thinks that Yang left his real father in pursue of a rich lifestyle, therefore he does everything he could to rebel against the new family.</p>\ 
          <p> Liu is about to turn 18, and Hu plans to throw a grand party for him, but Liu does not approve and insists on celebrating his birthday with his birth father. Back at home, Hu blames Yang for the fact that Liu doesn’t appreciate his gesture. Liu listened to Hu and Yang’s fight, and imagines himself raging into the room and fights Hu.</p>\ 
          <p> Hu threatens Yang and told her that if Liu still doesn’t accept him, she will no longer have the chance to attend social events with him. Yang tries to talk Liu into accepting Hu, but Liu accuses Yang for being greedy and betraying his birth father. </p>\ 
          <p> Next day, Hu goes out without Yang. Yang intends to make up with Hu and decides to clean the house. Neither Liu nor Qian wants to help. Qian is playing with her pet dog, but accidentally damaged one of Hu’s important documents. Later that night, Hu comes home; and to their surprise, Liu and Yang discover that Hu is having an affair. On the other hand, Hu is outrageous when he finds out that his documents are damaged. Qian pins the guilt on Yang and humiliates her. Now, Liu’s step-grandmother Fen took the blame for her. </p>\ 
     <p>Liu came to Qian’s room and asked her why did she blame the damaged documents on his mom. They fight, and Qian humiliates Liu and Yang carelessly. Liu throws himself onto Qian and pushes her onto her bed and tries to assault her. However, when Qian, who usually has a lot of pride, starts to beg, Liu loses his desire. At this time, Liu hears Hu and Yang’s steps walking towards the room, he imagines himself assaulting Qian in front of Hu. He walks out of the room when his thoughts came back.</p>\ 
     <p>Liu decides to leave the house to find his birth father until he his birthday passes. When he tries to sneak downstairs, he sees Hu is reading the newspaper with his back facing him while Yang is making tea. Liu quietly watches Yang taking a bag of power then pours it into the tea cup. Yang turns around and sees Liu, but calmly hands the cup of tea to Hu. The two of them watch Hu chuck the tea. Yang walks towards Liu emotionlessly, then Liu walks up the stairs with a shaking torso. </p>\ 
     <p>In his room, Liu is sitting on his bed and staring into the space. Yang slowly opens the door and enters, then she takes out a suitcase from underneath the bed. Liu sits still while Yang hustles through packing in silent. The entire house is quiet: Qian is in her bed, and the speaker in the corner plays music slowly; Fen is sleeping on a chair in her room, and her book falls on the floor from her hand; in the living room, Hu lies still against the couch without any movements.</p>\ 
     <p>Outside, the house stands tall in the tranquility, and the the light from the house slowly diminishes in the dark.</p> \ 
     <p>-End- </p>'); 
     chinese=false 
    }); 
    }; 
}); 

回答

0

您檢查語言是否處於活動狀態僅在文檔準備就緒時運行一次。
但是,您希望此檢查位於的點擊功能

$(document).ready(function() { 

    //initialize the variable once the document is ready 
    var chinese=false; 

    $('#tochinese').click(function() { 
    if (chinese === false) { 
     //translate the site into chinese 
     $('#tochinese').html('<span>View in English</span>'); 
     $('#menu').html('菜單'); 
    } 
    else { 
     //translate the site into english 
     $('#menu').html('Menu') 
     $('#tochinese').html('<span>切換到英文</span>'); 
    } 
    } 
} 

現在檢查的語言應該是主動發生在每一次點擊,而不是在文件準備功能一次。

0

你可以做到這一點有點不同,不使用中國的布爾可言。 您可以在HTML中製作2個按鈕,有點像這樣。

<a id='toChinese'>CHINESE</a> 
<a id='toEnglish' style='display:none;'>ENGLISH</a> 

由於'display:none'它只會一次顯示其中一個按鈕。然後當你想改變爲中文使用toChinese.click調用所有東西時,使css顯示none並顯示英文按鈕塊。這改變了按鈕,你可以將其轉換回英語以同樣的方式:

$("#toChinese").click(function() { 
    //change to chinese 
    $("#toChinese").css("display", "none"); 
    $("#toEnglish").css("display", "block"); 
}); 

$("#toEnglish").click(function() { 
    //change to English 
    $("#toEnglish").css("display", "none"); 
    $("#toChinese").css("display", "block"); 
});