2016-04-07 40 views
0

我試圖在單擊按鈕時在頁面上顯示「事實」。我將事實列表設置爲一個帶有循環的字符串數組,應該如下所示通過循環。試圖讓一個字符串數組按照順序打印出來,然後單擊一個按鈕

var textArray = [ 
"<strong>Fact 1:</strong> Sign language is the fourth most used language in the US.", 
"<strong>Fact 2:</strong> Deaf History Month is observed from March 13th to April 15th every year.", 
"<strong>Fact 3:</strong> There are hundreds of sign language dialects in use around the world. Each culture has developed its own form of sign language to be compatible with the language spoken in that country.", 
"<strong>Fact 4:</strong> A form of ASL has been used in the U.S. for over two hundred years.", 
"<strong>Fact 5:</strong> In the United States, many people with hearing impairments communicate by using American Sign Language, also known as ASL. ASL combines hand signs, gestures, and facial expressions to create words and sentences. As many as 500,000 people in the U.S. communicate using ASL. Not all ASL users are hearing impaired. Some are family members, friends, or teachers of people with hearing impairments.", 
"<strong>Fact 6:</strong> You might think that modern-day ASL originated in England, but it did not. It came from France. England has its own version of sign language that is very different from ASL. An American who only knows ASL will have a hard time communicating with someone from England who only knows Modern British Sign Language. But a person using ASL has a good chance of being able to communicate with a person using French Sign Language - even if they don't speak French!", 
"<strong>Fact 7:</strong> People with hearing impairments have been using signed languages for a very long time. In the 18th century in France, some people did not think children with hearing impairments should go to school. They thought that since they could not speak or hear, they would be unable to learn. A man with a hearing impairment named Pierre Desloges believed these people were wrong. He wrote a book that described the signed language used by people with hearing impairments in Paris. This book helped to change the minds of many people. Soon there were schools in France for children with hearing impairments. ", 
"<strong>Fact 8:</strong> Ludvig van Beethoven was a German composer and pianist. He started going deaf at the age of 28 and by age 49 he could no longer hear. He continued to get worse but went on composing music. Beethoven died in 1857 when he was 57 years-old.", 
"<strong>Fact 9:</strong> There are hundreds of sign language dialects in use around the world. Each culture has developed its own form of sign language to be compatible with the language spoken in that country.", 
"<strong>Fact 10:</strong> Babies can communicate physically 6-8 months  prior to communicate verbally." 


]; 

function factClick(){ 

var arrayLength = textArray.length; 
for (var i = 0; i < arrayLength; i++) { 
    document.getElementById("factID").innerHTML = textArray[i]; 

    } 
} 

而且它應該打印出被點擊我的頁面(http://thecodingninja.com/muslim/facts.php)上的按鈕時。

下面

<script src="facts.js"></script> 

<center> 
<p id="factID" style="color:white;"> test <p> 
<button onclick="factClick()">Change Text</button> 

</center> 

代碼爲什麼不工作?

如果更容易,您可以在jQuery中給出答案。

+0

控制檯中的任何錯誤? – Rayon

+0

不,不存在 – MuslimTwin

+0

任何特定的原因PHP被標記在這個問題上?你發佈的所有代碼似乎都是客戶端的東西。 –

回答

0

發生了什麼變化:

  • CSS顏色現在是黑色(白色白不顯示。)
  • 外功能計數器,因爲元素必須堅持。
  • 計數器更正。
  • 因此沒有for循環。

var textArray = [ 
 
     "<strong>Fact 1:</strong> Sign language is the fourth most used language in the US.", 
 
     "<strong>Fact 2:</strong> Deaf History Month is observed from March 13th to April 15th every year.", 
 
     "<strong>Fact 3:</strong> There are hundreds of sign language dialects in use around the world. Each culture has developed its own form of sign language to be compatible with the language spoken in that country.", 
 
     "<strong>Fact 4:</strong> A form of ASL has been used in the U.S. for over two hundred years.", 
 
     "<strong>Fact 5:</strong> In the United States, many people with hearing impairments communicate by using American Sign Language, also known as ASL. ASL combines hand signs, gestures, and facial expressions to create words and sentences. As many as 500,000 people in the U.S. communicate using ASL. Not all ASL users are hearing impaired. Some are family members, friends, or teachers of people with hearing impairments.", 
 
     "<strong>Fact 6:</strong> You might think that modern-day ASL originated in England, but it did not. It came from France. England has its own version of sign language that is very different from ASL. An American who only knows ASL will have a hard time communicating with someone from England who only knows Modern British Sign Language. But a person using ASL has a good chance of being able to communicate with a person using French Sign Language - even if they don't speak French!", 
 
     "<strong>Fact 7:</strong> People with hearing impairments have been using signed languages for a very long time. In the 18th century in France, some people did not think children with hearing impairments should go to school. They thought that since they could not speak or hear, they would be unable to learn. A man with a hearing impairment named Pierre Desloges believed these people were wrong. He wrote a book that described the signed language used by people with hearing impairments in Paris. This book helped to change the minds of many people. Soon there were schools in France for children with hearing impairments. ", 
 
     "<strong>Fact 8:</strong> Ludvig van Beethoven was a German composer and pianist. He started going deaf at the age of 28 and by age 49 he could no longer hear. He continued to get worse but went on composing music. Beethoven died in 1857 when he was 57 years-old.", 
 
     "<strong>Fact 9:</strong> There are hundreds of sign language dialects in use around the world. Each culture has developed its own form of sign language to be compatible with the language spoken in that country.", 
 
     "<strong>Fact 10:</strong> Babies can communicate physically 6-8 months prior to communicate verbally." 
 
    ], 
 
    i = 0; 
 

 
function factClick(){ 
 
    document.getElementById("factID").innerHTML = textArray[i]; 
 
    i++; 
 
    i %= textArray.length; // keep in right range 
 
}
<center> 
 
<p id="factID" style="color:black;"> test <p> 
 
<button onclick="factClick()">Change Text</button> 
 
</center>

0

你在這裏使用for循環。請記住,一個for循環將一次全部執行。所以你一個接一個地印出每一個事實,但這一切都是在眨眼之間發生的。到達循環結束時,您已經打印出所有的事實,而最後打印的是所有的事實。

如果您想要打印出新的事實,並按順序進行操作,並且每次按下按鈕時只打印一個新事實,則不需要此處的for循環。相反,你希望有一個簡單的計數器,每次按下按鈕時增加它,並用它來跟蹤顯示哪個事實。

var textArray = []; // your array, imagine it's full of 10 facts 
var counter = 0; // use this to track which fact to display 
function fastClick(){ 
    document.getElementById("factID").innerHTML = textArray[counter]; 
    counter++; 
    if(counter > textArray.length - 1){ 
     counter = 0; // make sure counter isn't bigger than array length 
    } 
} 
相關問題