向 java 脚本字符串添加样式



我创建了一个在JavaScript中生成随机文本的按钮。我想将随机生成的文本样式设置为位于按钮下方,居中,并具有以下颜色:#8a2be2。


<!-- arrays -->
function GetValue()
{
var myarray= new Array();
	myarray[0]="The calcium in our bones and the iron in our blood come from ancient explosions of giant stars."
	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-->	
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>

在p或div元素中添加文本作为波纹管


<!-- arrays -->
function GetValue()
{
var myarray= new Array();
	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>"
	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-->	
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>


<!-- arrays -->
function GetValue()
{
var myarray= new Array();
	myarray[0]="The calcium in our bones and the iron in our blood come from ancient explosions of giant stars."
	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-->	
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>标记中,如上所示,或者导入外部样式表。

您可以使用document.getElementById("fact button").style.[property] = "[value]";来设置不同的样式。以下代码演示如何实现所需目标。


<!-- arrays -->
function GetValue()
{
var myarray= new Array();
	myarray[0]="The calcium in our bones and the iron in our blood come from ancient explosions of giant stars."
	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-->	
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>

最新更新