2015-10-16 78 views
-4

我很困惑,爲什麼它沒有進入函數。起初,我認爲這可能是因爲innerHTML的東西而掛起來的,但那不可能,因爲它甚至沒有進入功能。我知道這是下面的代碼有問題,而不是iframe源代碼中的頁面,因爲它們工作得很好,問題正在發生。由於某種原因沒有調用JavaScript函數

<html> 
<head> 
<script> 
function setFrameContent(num){ 



window.alert("Ow!"); 

if (num == 1) 
{ 

document.getElementById("div1").innerHTML = " 
<iframe src='./cosTweets1.html' style='width: 1000px; height: 190px;'></iframe>"; 


} 

else if (num == 2) 
{ 

document.getElementById("div1").innerHTML = " 
<iframe src='./cosTweets2.html' style='width: 1000px; height: 190px;'></iframe>"; 

} 


} 
</script> 
</head> 
<body> 




<div style="border: 1px solid black; width: 1000px; background-color: blue;"> 
<button id="b1" value="1" style="width: 90px;" onclick="setFrameContent(1)"> 1</button> 
<button id="b2" value="2" style="width: 90px;" onclick="setFrameContent(2)"> 2</button> 
<button id="b3" value="3" style="width: 90px;"> 3</button> 
<button id="b4" value="4" style="width: 90px;"> 4</button> 
<button id="b5" value="5" style="width: 90px;"> 5</button> 
<button id="b6" value="6" style="width: 90px;"> 6</button> 
<button id="b7" value="7" style="width: 90px;"> 7</button> 
<button id="b8" value="8" style="width: 90px;"> 8</button> 
<button id="b9" value="9" style="width: 90px;"> 9</button> 
<button id="b10" value="10" style="width: 90px;"> 10</button> 

</div> 

<div id="div1" style="border: 1px solid black; width: 1000px;"> 

<iframe src="./cosTweets1.html" style="width: 1000px; height: 190px;"></iframe> 

</div> 



</body> 
</html> 

奇怪的是,這個其他代碼下面我工作,爲另一件事,沒有工作,所以也不能說我剛過了一個數字,導致該問題。

<html> 
<head><title> SPRBC Events Demo </title> 

<style> 

p { font-size: 2.5em;} 
</style> 
<script> 

var array = ["Bla bla bla", "Yak yak yak", "Mongoose", "We need more funds!"]; 


function changeText(index) 
{ 

document.getElementById("p1").innerHTML = 

array[index]; 

} 


</script> 
</head> 
<body> 

<p id="p1" > So frustrating! </p> 

<a href="#" onclick="changeText(0)">Click</a> 
<a href="#" onclick="changeText(1)">Click</a> 
<a href="#" onclick="changeText(2)">Click</a> 
<a href="#" onclick="changeText(3)">Click</a> 
</body> 
</html> 
+1

你應該把小提琴。 – csmckelvey

+1

我同意takendarkk。你必須至少爲它做一個小提琴。 – Jason

+1

什麼是應該工作,如何,但不是?我在這裏看不到代碼,我沒有明白你的觀點。 – Lucio

回答

4

JavaScript中的字符串不能多於一行,但數組可以。這就是爲什麼你的第一個代碼示例不起作用,而你的第二個代碼示例不行

使用

document.getElementById("div1").innerHTML = "<iframe src='./cosTweets1.html' style='width: 1000px; height: 190px;'></iframe>"; 

document.getElementById("div1").innerHTML = "<iframe src='./cosTweets2.html' style='width: 1000px; height: 190px;'></iframe>"; 
相關問題