DOM -如何移动HTML元素
DOM - How to move HTML elements?
我想创建一个新元素(a div
),但不是将其创建为最后一个元素,而是将其创建在两个元素之间。我创建了下面这个简化的代码来表示我想要做的事情:
<!DOCTYPE html>
<html lang="ca">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
function createDiv() {
var newDiv = document.createElement("div");
var txt = document.createTextNode("I'm the second div");
newDiv.appendChild(txt);
document.body.appendChild(newDiv);
}
</script>
</head>
<body>
<div class="first">
<p>I'm the first div</p>
</div>
<div class="third">
<p>I'm the third div</p>
</div>
<button type="button" name="button" onclick="createDiv()">Create the second Div</button>
</body>
</html>
请记住,我只想使用DOM,而不是jQuery
您可以通过在第三个div
之前插入以下操作
function createDiv() {
var newDiv = document.createElement("div");
var txt = document.createTextNode("I'm the second div");
newDiv.appendChild(txt);
var thirdDiv = document.getElementById("thrid");
thirdDiv.parentNode.insertBefore(newDiv, thirdDiv);
}
<!DOCTYPE html>
<html lang="ca">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="first">
<p>I'm the first div</p>
</div>
<div id="thrid" class="third">
<p>I'm the third div</p>
</div>
<button type="button" name="button" onclick="createDiv()">Create the second Div</button>
</body>
</html>
相关文章:
- 当#在iFrame中使用HTML时,阻止页面移动
- 移动HTML输入文本框在提交时隐藏软键盘
- 使用jquery为移动布局更改html层次结构
- 移动html/javascript编程中的变量传递
- html视频javascript播放方法在移动Safari中不起作用
- 录制/跟踪html滑块控制移动
- 当我移动引用three.js的html文件时,three.js停止工作
- 根据 URL 中的哈希移动 HTML
- HTML 不加载 CSS ,刷新后一切正常.同样在移动设备上,它在首次加载时不起作用
- 如何在 HTML 中将幻灯片移动到页面中心
- 移动HTML:触摸结束后继续滚动
- 可以使用jQuery'移动HTML背景吗;s设置动画
- 如何在不移动 html 中周围元素的情况下扩展图像
- 从移动 HTML 主体在方向更改时调用 Javascript 函数
- 使用 JavaScript 移动 html td 元素
- 有条件地移动html元素
- 在页面之间移动html
- DOM -如何移动HTML元素
- 移动html &点击img时,主体从上到上
- 在移动设备上使用javascript移动html的性能问题