<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>复制节点/替换节点</title>
<style>
b.off{
background: #ff0;
}
</style>
<script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
// window.onload=function(){
// var div = document.getElementsByTagName("div")[0];
// div.onclick = function(){
// var div1 = div.cloneNode(true);
// div.parentNode.insertBefore(div1,div.nextSibling);
// }
// 注意:复制的div元素不拥有事件处理函数,但是拥有div标签包含的事件属性.
// 替换节点
// var p = document.getElementsByTagName("p");
// // console.log(p.length);
// var div = document.createElement("div");
// div.innerHTML = "盒子";
// var len = p.length;
// for (var i = 0; i < len; i++) {
// // console.log(p.length);
// var div1 = div.cloneNode(true);
// p[i].parentNode.replaceChild(div1,p[i]);
// }
// }
$(function(){
// $("b").click(function(){
// // $(this).toggleClass("off");
// // 成倍增加
// $("b").clone(true).insertAfter("span");
// });
$("button").click(function(){
// $(this).replaceWith("<div>"+$(this).text()+"</div>");
// $(this).replaceWith(function(){
// return "<div>"+$(this).text()+"</div>";
// });
$("<div>"+$(this).text()+"</div>").replaceAll(this);
});
});
</script>
</head>
<body>
<!-- <div class="bg" ondblclick="alert('123')">
<p>段落文字</p>
</div>
<b>加粗文字</b>
<p>hello</p>
<span>jqueryClone+</span> -->
<!-- <p>段落1</p>
<p>段落2</p>
<p>段落3</p> -->
<button>按钮1</button>
<button>按钮2</button>
<button>按钮3</button>
</body>
</html>
复制节点/替换节点
最新推荐文章于 2023-03-08 22:43:05 发布