日期:2014-05-16  浏览次数:20399 次

javascript往head标记中追加内容
想用javascript向网页的<head></head>标记中追加内容,不知如何实现,注意是追加,不是将head间的内容替换掉!
初学javascript,忘多指教!

------解决方案--------------------
HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<script type="text/javascript">
var head = document.getElementsByTagName('head')[0];

//方法一:动态创建并添加节点
var obj = document.createElement('script');
obj.type = 'text/javascript';
obj.src = 'http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js';
head.appendChild(obj);

//方法二:修改head对象的innerHTML
var str = '<link rel="stylesheet" href="http://static.jquery.com/files/rocker/css/reset.css" type="text/css" />';
head.innerHTML += str;
</script>
</body>
</html>