2017-08-16 26 views
2

我創建了一個按鈕,該按鈕在JavaScript中生成隨機文本。我想將隨機生成的文本設置爲按鈕下方的居中,並具有以下顏色:#8a2be2。將樣式添加到java腳本字符串


 
<!-- arrays --> 
 
function GetValue() 
 
{ 
 
    var myarray= new Array(); 
 

 
\t myarray[0]="The calcium in our bones and the iron in our blood come from ancient explosions of giant stars." 
 
\t myarray[1]="The Chinese giant salamander can grow to be 6 feet (1.8 m) long, making it the largest salamander in the world." 
 
<!--END--> \t 
 
    var random = myarray[Math.floor(Math.random() * myarray.length)]; 
 
    //alert(random); 
 
    document.getElementById("fact button").innerHTML=random; 
 
}
<input type="button" id="fact_button" value="Fact Button" onclick="GetValue();" /> 
 
<p id="fact button" ></p> 
 
<h2 class="h2">click fact button for an amazing fact!</class> </h2>

+1

你嘗試過什麼迄今爲止應用樣式,並且你想要的風格是什麼?按鈕或字符串,因爲你的問題似乎有點混淆你想要的樣式... – NewToJS

+0

你可以添加CSS樣式,你面臨的問題是什麼? – Dij

+0

是的,我添加了一個CSS檢查出來 –

回答

1

裏面添加p文本或div元素如波紋管


 
<!-- arrays --> 
 
function GetValue() 
 
{ 
 
    var myarray= new Array(); 
 

 
\t myarray[0]="<p style='color:#8a2be2'>The calcium in our bones and the iron in our blood come from ancient explosions of giant stars.</p>" 
 
\t myarray[1]="<p style='color:#8a2be2'>The Chinese giant salamander can grow to be 6 feet (1.8 m) long, making it the largest salamander in the world.</p>" 
 
<!--END--> \t 
 
    var random = myarray[Math.floor(Math.random() * myarray.length)]; 
 
    //alert(random); 
 
    document.getElementById("fact button").innerHTML=random; 
 
}
<input type="button" id="fact_button" value="Fact Button" onclick="GetValue();" /> 
 
<p id="fact button" ></p> 
 
<h2 class="h2">click fact button for an amazing fact!</class> </h2>

+0

所以這將是一個段落內的段落....爲什麼不只是風格的第一段,而不是嵌套他們申請風格......你的方法沒有意義。 – NewToJS

+0

您還可以通過代碼更改款式或爲段落 –

1


 
<!-- arrays --> 
 
function GetValue() 
 
{ 
 
    var myarray= new Array(); 
 

 
\t myarray[0]="The calcium in our bones and the iron in our blood come from ancient explosions of giant stars." 
 
\t myarray[1]="The Chinese giant salamander can grow to be 6 feet (1.8 m) long, making it the largest salamander in the world." 
 
<!--END--> \t 
 
    var random = myarray[Math.floor(Math.random() * myarray.length)]; 
 
    //alert(random); 
 
    document.getElementById("fact button").innerHTML=random; 
 
}
<input type="button" id="fact_button" value="Fact Button" onclick="GetValue();" /> 
 
<p id="fact button" style="text-align:center;color:#8a2be2;"></p> 
 
<h2 class="h2">click fact button for an amazing fact!</class> </h2>

如果您想要應用css樣式,您可以簡單地將它們添加到<p>標籤,如上所示或導入外部樣式表。

+0

添加道具給你布魯它的工作! –

+0

如果你能接受答案,那會很棒! – manpozi

1

您可以使用document.getElementById("fact button").style.[property] = "[value]";設置不同的樣式。以下代碼顯示了您可以如何實現所需的目標。


 
<!-- arrays --> 
 
function GetValue() 
 
{ 
 
    var myarray= new Array(); 
 

 
\t myarray[0]="The calcium in our bones and the iron in our blood come from ancient explosions of giant stars." 
 
\t myarray[1]="The Chinese giant salamander can grow to be 6 feet (1.8 m) long, making it the largest salamander in the world." 
 
<!--END--> \t 
 
    var random = myarray[Math.floor(Math.random() * myarray.length)]; 
 
    //alert(random); 
 
    document.getElementById("fact button").innerHTML=random; 
 
    document.getElementById("fact button").style.color ="#8a2be2"; 
 
    document.getElementById("fact button").style.textAlign="center"; 
 
}
<input type="button" id="fact_button" value="Fact Button" onclick="GetValue();" /> 
 
<p id="fact button" ></p> 
 
<h2 class="h2">click fact button for an amazing fact!</class> </h2>