将样式添加到java脚本字符串
我创建了一个按钮,该按钮在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>
里面添加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>
所以这将是一个段落内的段落....为什么不只是风格的第一段,而不是嵌套他们申请风格......你的方法没有意义。 – NewToJS
您还可以通过代码更改款式或为段落 –
<!-- 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>
标签,如上所示或导入外部样式表。
添加道具给你布鲁它的工作! –
如果你能接受答案,那会很棒! – manpozi
您可以使用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>
你尝试过什么迄今为止应用样式,并且你想要的风格是什么?按钮或字符串,因为你的问题似乎有点混淆你想要的样式... – NewToJS
你可以添加CSS样式,你面临的问题是什么? – Dij
是的,我添加了一个CSS检查出来 –