将HTML元素位置更改为其他位置javascript
我想使用img.fnone并通过JavaScript或jquery将其替换到span.ddlabel元素下 有什么功能可以做到这一点吗将HTML元素位置更改为其他位置javascript,javascript,jquery,html,dom,Javascript,Jquery,Html,Dom,我想使用img.fnone并通过JavaScript或jquery将其替换到span.ddlabel元素下 有什么功能可以做到这一点吗 <span id="flat_title" class="ddTitleText "> <img class="fnone" src="images/pinc.png"> <span class="ddlabel">A</span> <span class="description
<span id="flat_title" class="ddTitleText ">
<img class="fnone" src="images/pinc.png">
<span class="ddlabel">A</span>
<span class="description" style="display: none;"></span>
</span>
A.
使用
$(文档).ready(函数(){
$('span.ddlabel')。追加(“”);
$(“#平面标题img:first”).remove()
});
使用element.appendChild方法
<span id="flat_title" class="ddTitleText ">
<img class="fnone" src="images/pinc.png" />
<span class="ddlabel">A</span>
<span class="description" style="display: none;"></span>
</span>
<div>
<button id="abc">Click to replace</button>
</div>
<script>
document.getElementById('abc').addEventListener('click',function(){
var parent = document.getElementById('flat_title'),
img = document.getElementsByClassName('fnone')[0],
description = document.getElementsByClassName('description')[0];
parent.appendChild(img);
parent.appendChild(description);
})
</script>
A.
单击以替换
document.getElementById('abc')。addEventListener('click',function(){
var parent=document.getElementById('flat_title'),
img=document.getElementsByClassName('fnone')[0],
description=document.getElementsByClassName('description')[0];
父母、子女(img);
父、子(描述);
})
在使用html()上检查它
为什么要费心克隆和删除?如果你把它附加到其他地方,它只是移动,它不是一个副本。是的,它工作得很好,但是如果页面中有许多元素具有相同的类名,而我想做同样的事情呢action@thehero75为此,您必须使用
ID
,或在每个元素上循环,并找到要将其移动到的正确项。
<script type="text/javascript">
$(document).ready(function () {
$('span .ddlabel').append('<img class="fnone" src="images/pinc.png">');
$('#flat_title img:first').remove()
});
</script>
<span id="flat_title" class="ddTitleText ">
<img class="fnone" src="images/pinc.png" />
<span class="ddlabel">A</span>
<span class="description" style="display: none;"></span>
</span>
<div>
<button id="abc">Click to replace</button>
</div>
<script>
document.getElementById('abc').addEventListener('click',function(){
var parent = document.getElementById('flat_title'),
img = document.getElementsByClassName('fnone')[0],
description = document.getElementsByClassName('description')[0];
parent.appendChild(img);
parent.appendChild(description);
})
</script>
var html=$("img.fnone").html();
$("span.ddlabel").html(html);
$("img.fnone").remove();