2017-05-07 71 views
-1

我正嘗試使用Javascript將頁面翻譯成拉丁字母。我有翻譯的邏輯,但我無法獲取該段落。 我的網頁有兩個段落和一個翻譯按鈕。當按鈕被點擊時,應該用拉丁文中的相同段落打開一個新窗口。 以下是我的代碼:在Javascript中將段落轉換爲拉丁字母

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> 
<title> String Manipulation </title> 
<head> 
<center><h1 style="color:Blue; font-family:verdana;"> BURJ KHALIFA</h1> <hr> 
<img src ="burjkhalifa.jpeg" width = "650" height ="370"></center> 
<script type= "text/javascript"> 

    function getTranslate() 
     { 
     var text = document.getElementById("pagetitle").value; 
     newWindow = window.open(text, "myWindow"); 
     var newWindowBody = newWindow.document.getElementsByTagName("body")[0]; 
     newWindowBody.setAttribute("onload", "window.opener.getParagraphs()"); 
     } 

    function getParagraphs() { 
     var pElement = newWindow.document.getElementsByTagName("p"); 
     var str=''; 
     for(var counter =0; counter < pElement.length;counter++) 
     { 
      document.writeln(translate(pElement[counter].innerHTML)); 
      document.writeln("<p>&nbsp;</p>") 

     } 

} 

    function translate(word) 
    { 
     var array = word.split(''); 
     var vowels = ['a','e','i','o','u']; 
     var newWord = ''; 
     for(var i = 0; i < vowels.length-1; i++) 
    { 
     for(var y = 0; y < array.length-1; y++) 
     { 
      if(array[y] == vowels[i]) 
      { 
       for(var x = y; x < array.length; x++) 
       { 
       newWord = newWord + array[x]; 
       } 
       for(var n = 0; n < y; n++) 
       { 
        newWord = newWord + array[n]; 
       } 
       newWord += "ay"; 
      }  
     } 
    } 
    } 
    return newWord; 
</script> 
</head> 

<body> 

<p style="font-family:courier; font-size:100%;"> 
The Burj Khalifa, known as the Burj Dubai before its inauguration, is a megatall skyscraper in Dubai, 
United Arab Emirates. It has a roof height of 828 m (2,717 ft), and with its antenna included, it stands 
a total height of 829.8 m (2,722 ft), making it the tallest building and the tallest structure in the world. 
</p> 

<p style="font-family:courier; font-size:100%;"> 
Construction of the Burj Khalifa began in 2004, with the exterior completed 5 years later in 2009. The primary 
structure is reinforced concrete. The building was opened in 2010 as part of a new development called Downtown Dubai. 
It is designed to be the centrepiece of large-scale, mixed-use development. The decision to build the building is 
reportedly based on the government's decision to diversify from an oil-based economy, and for Dubai to gain international 
recognition. The building was named in honour of the ruler of Abu Dhabi and president of the United Arab Emirates, Khalifa 
bin Zayed Al Nahyan; Abu Dhabi and the UAE government lent Dubai money to pay its debts. The building broke numerous height 
records, including its designation as the tallest tower in the world. 
</p> 


<input type="button" id = "submit" name="" value="Translate" style="color: green; font-size:18pt"size ="10" onclick ="getTranslate()" /> 

</html> 
+0

您在瀏覽器的控制檯中遇到什麼錯誤?在旁註中,您似乎錯過了一個結束標籤,並使用了不存在的'

'標籤。 – j08691

+0

我無法讀取段落。當我點擊翻譯按鈕時,沒有任何反應。 –

+0

打開瀏覽器的開發人員工具(F12)並查看它在控制檯中的含義 – j08691

回答

0

有你的代碼的幾個問題:

1. return newWord應該是translate功能

2. var text = document.getElementById("pagetitle").value;返回空裏面,因爲沒有與元素「pagetitle」的編號

3.right在您的<title>之後,還有一個額外的開口<head>元素

嘗試在Firefox或Chrome的控制檯中查看是否有任何錯誤。