我试图不改变一个段落的id属性的按钮点击。第一部分起作用。但是当按钮再次被点击时,应该恢复到原来的样式。新的与JavaScript,所以任何其他有帮助的文章将是很好的。
下面是css:
<style type="text/css">
#attention
{
font-size:24px;
background-color:yellow;
width:300px;
}
#normal
{
background-color:white;
font-size:12px;
}
</style>JS
function changeText() {
var text = document.getElementById("textChange");
text.innerHTML = "New Text";
text.id = "attention";
}HTML
<div>
<p id="textChange">Some generic text here</p>
<input id="Button1" type="button" value="Do Something" onclick="changeText();" />
</div>发布于 2014-04-01 21:26:48
CSS
<style type="text/css">
.attention
{
font-size:24px;
background-color:yellow;
width:300px;
}
.normal
{
background-color:white;
font-size:12px;
}
</style> <div>
<p id="textChange">Some generic text here</p>
<input id="Button1" type="button" value="Do Something" />
</div>JS
var btn = document.getElementById('Button1');
btn.addEventListener('click', function() {
var div = document.getElementById('textChange');
div.className = 'attention';
});发布于 2014-04-01 21:22:49
您应该为这类功能使用类。例如:
function changeText() {
var text = document.getElementById("textChange");
text.innerHTML = "New Text";
text.className = text.className == "attention" ? 'normal' : 'attention';
}因此CSS变成:
.attention {
font-size:24px;
background-color:yellow;
width:300px;
}
.normal {
background-color:white;
font-size:12px;
}https://stackoverflow.com/questions/22797087
复制相似问题