<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>改变html样式</title>
</head>
<body>
<h1 id="con">这是一首简单的小情歌</h1>
<script type="text/javascript">
var char=document.getElementById("con");
char.style.color="black";
char.style.backgroundColor="red";
char.styke.fontsize="25px";
char.style.width="100px";
char.style.margin="auto";
</script>
</body>
</html>
补充上面回答的问题,没看清楚您问的是字体居中,以下是效果和代码,望采纳:
您写的源码有一个style写错了,所以下面的属性没执行,其实您的效果已经居中了
代码:
效果:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>练习页面</title>
<script type="text/javascript" src="jQuery/jquery.js"></script>
<style type="text/css">
* {
margin: 0px;
padding: 0px;
}
//加个css样式就好了
h1 {
text-align: center;
}
</style>
<script>
</script>
</head>
<body>
<h1 id="con">这是一首简单的小情歌</h1>
<script type="text/javascript">
var char = document.getElementById("con");
char.style.color = "black";
char.style.backgroundColor = "red";
char.styke.fontsize = "25px";
char.style.width = "100px";
char.style.margin = "auto";
</script>
</body>
</html>