<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>className属性</title>
<style>
body{ font-size:16px;}
.one{
border:1px solid #eee;
width:230px;
height:50px;
background:#ccc;
color:red;
}
.two{
border:1px solid #ccc;
width:230px;
height:50px;
background:#9CF;
color:blue;
}
</style>
</head>
<body>
<p id="p1" > JavaScript使网页显示动态效果并实现与用户交互功能。</p>
<input type="button" value="添加样式" onclick="add()"/>
<p id="p2" class="one">JavaScript使网页显示动态效果并实现与用户交互功能。</p>
<input type="button" value="更改外观" onclick="modify()"/>
<input type="button" value="清除样式" onclick="clean()"/>
<script type="text/javascript">
function add(){
var p1 = document.getElementById("p1");
p1.className = "one";
}
function modify(){
var p2 = document.getElementById("p2");
p2.className = "two";
}
function clean(){
var deletestyle = confirm('是否清除样式')
if(deletestyle == true){
document.getElementById("p2").removeAttribute("style");
}
else{
alert("啥都不做");
}
}
</script>
</body>
</html>removeAttribute()方法用来删除指定的属性,是.style的,在<style></style>标签里面改的样式好像重置不了
不知道是不是这样
可以去做产品啊~~
msaotbqtpjfyaqcooaqtwsrefavymzqqslfwxjtrjxdticnldqwvqkuiqkbhchsxeosxrvqqlogxcsqvbdhvwecihgdzopegwyhrnnkhfhqnrmxigeubexuoobboyno
楼主我知道了 ,在<style></style>标签里面改的样式是清除不了的,可是你知道怎么改吗
请问楼主这个问题如何解决啊,我也是有这个困扰
也许是因为。。你removeAttribute("style"),style的值没有写在P1和P2标签里面。是写在head上面的。所以。我觉得可以这样写
function clean(){
var deletestyle = confirm('是否清除样式')
if(deletestyle == true)
{
p1.className = "";
p2.className = "one";
}
else{
alert("啥都不做");
}