<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>JS代码</title>
</head>
<body>
<script type="text/javascript">
//1.创建好标签
var h2 = document.createElement('h2');
//2.创建文本节点
var text = document.createTextNode("我是JS创建出来的");
//3.创建属性节点
var attr = document.createAttribute("align");
//4.为属性节点赋值
attr.value = 'center';
//5.为h2元素添加属性节点
h2.setAttributeNode(attr);
//6.为h2元素添加文本节点
h2.appendChild(text);
//7.为h2节点追加为body元素的子
document.querySelector('body').appendChild(h2);
</script>
</body>
</html>
————————————————————————————————
使用JQ前要下载Jquery.min.js、1.7.2的简洁版本就够用了
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jquery代码</title>
<script type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$('<h2 align="center">我是jquery创建的</h2>').appendTo('body');
</script>
</body>
</html>