北条纱希
2015-08-04 14:40
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>
<body>
<div><b id="oldnode">JavaScript</b>是一个很常用的技术,为网页添加动态效果。</div>
<a href="javascript:replaceMessage()"> 将加粗改为斜体</a>
<script type="text/javascript">
function replaceMessage(){
var oldnode = document.getElementById("oldnode");
var newnode=document.creatElement("i");
oldnode.parentNode.replaceChild(newnode,oldnode);
</script>
</body>
</html>为什么这个不对啊?
href中那样写是没有错的 你这个代码少了一条,你没有给你创建的新标签定义内容 如下:
function replaceMessage(){
var oldp = document.getElementById('oldnode');
var newi = document.createElement("i");
newi.innerHTML = oldp.innerHTML;
oldp.parentNode.replaceChild(newi,oldp);
}
函数定义你的{ }不完全,粗体换为斜体 你得先获取innerHtml再给新增加的i标签设定值 以上
href是指向地址的。。。。。你要用onclick=()吧
JavaScript进阶篇
469074 学习 · 22582 问题
相似问题