一个html页面。里面有一个:div标签。
<div id="div1" onclick="func()" >我是一个div</div>请问,如何使用js代码,实现点击:“我是一个div”后,整个div标签变为以下代码:
<img id="img1" src="" />请问这个js代码如何写呢?请写出具体代码。谢谢。
<div id="div1" onclick="func()" >我是一个div</div>请问,如何使用js代码,实现点击:“我是一个div”后,整个div标签变为以下代码:
<img id="img1" src="" />请问这个js代码如何写呢?请写出具体代码。谢谢。
你外面添加一个div或者什么的 然后替换其innerHTML什么的不就可以了么
写个ff的扩展就可以了~~。
只是你不觉得这个很麻烦吗?
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<div id="div1" onclick="func()" >我是一个div</div>
<script>
$("#div1").replaceWith('<img id="img1" src="http://c.csdn.net/bbs/t/5/i/pic_logo.gif" />');
</script>
</body>
</html>
function func() {
var div = document.getElementById("div1");
div.parentNode.removeChild(div);
var img = document.createElement("img");
img.id = "img1";
img.src = "";
document.body.appendChild(img);
}
</script></head>
<body>
<div id="div1" style="background-color: lightgreen" onclick="func()">
我是一个div</div>
</body>
考虑外层dom中有数个dom元素
而这个div在中间
如果没有这么多的dom元素的话 直接div.parentNode.innerHTML = '';就可以了 对吧大叔的方法可行。如果楼主愿意使用jQuery的话。
var img=document.createElement("img");
img.id="img1";img.src="";
var par=div.parentNode;
par.insertBefore(img,div);
div.removeNode(true);
原生JS代码,未经测试...
function func() {
var div = document.getElementById("div1");
var img = document.createElement("img");
img.id = "img1";
img.src = ""; document.body.replaceChild(img,div);
}
</script></head>
<body>
<div id="div2" style="background-color: red">
我是一个div</div>
<div id="div1" style="background-color: lightgreen" onclick="func()">
我是一个div</div>
<div id="div3" style="background-color: blue">
我是一个div</div>
</body>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
function func()
{
var div1 = document.getElementById("div1");
div1.parentNode.removeChild(div1);
var img = document.createElement("img");
img.id = "img1";
img.src = "http://www.baidu.com/img/baidu_logo.gif";
document.body.replaceChild(img,div1);
}
</script>
</head>
<body>
<div id="div1" style="background-color: lightgreen" onclick="func()">我是一个div1</div>
<div id="div2" style="background-color: lightgreen" >我是一个div2</div>
</body>
</html>
<!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=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
function func(div)
{
var img=document.createElement("img");
img.id="img1";img.src="http://www.baidu.com/img/baidu_logo.gif";
var par=div.parentNode;
par.insertBefore(img,div);
div.removeNode(true);
}
</script>
</head>
<body>
<div id="div1" style="background-color: lightgreen" onclick="func(this)">我是一个div1</div>
<div id="div2" style="background-color: lightgreen" onclick="func(this)">我是一个div2</div>
</body>
</html>
我第二个代码删了这个的