如何将锚标记从一个地方附加到另一个地方

How to append anchor tag from one place to another?

本文关键字:一个 方附加 另一个      更新时间:2023-09-26

我已经写了

<div id="sample">
....
....
<a class="uploadedfiles" href="www.google.com">File</a>
.....
.....
<div class="diagram"></div>
.....
.....
.....      
</div>

现在我希望锚标记类上传的文件应该追加到用jQuery绘制类图输出应该是

<div class="diagram"><a href="www.google.com">File</a></div>

或者直接

$('.diagram').append($('.uploadedfiles').removeAttr('class'))

如果你有很多链接要添加到图表div中,你可以使用

$('.uploadedfiles').each(function(){
    $(this).appendTo('.diagram').removeAttr('class')
})
document.getElementsByClassName("diagram")[0].innerHTML+='<a href="www.google.com">File</a>';

我建议你这样做:

var diagram=document.getElementsByClassName('diagram')[0];
var uploadedfiles=document.getElementsByClassName('uploadedfiles');
var l=uploadedfiles.length;
for(var i=0;i<l;i++){
diagram.innerHTML+=uploadedfiles[i].outerHTML;
uploadedfiles[i].parentNode.removeChild(uploadedfiles[i]);
}

此代码删除类'uploadedfiles'的所有节点,并将其添加到'diagram'节点。

编辑:对不起,没有注意到你想要jQuery代码。我更喜欢纯js编码,所以我无法帮助您使用jQuery。但我认为其他答案是正确的。这段代码对那些不使用jQuery或其他js库的人很有帮助;)

这很简单,你只需要为每个锚启动word uploader进行搜索。

代码如下:

HTML:

<div id="sample">
<a class="uploadedfiles" href="www.google.com">File</a>
<div class="diagram"></div>
</div>

JS:

$("a[class^='uploaded']").appendTo('.diagram'); 

给你:http://jsfiddle.net/PLNH8/

谢谢,Ashok

OK..

你是在要求这样的东西吗?

<div id="sample">
<a class="uploadedfiles" href="www.google.com">File</a>
<div class="diagram"></div>
</div>
CSS

.diagram { width:100px;height:100px;border:1px solid #000000;}

JS

$(document).ready(function(){
    var url = $('.uploadedfiles').attr('href');
    $('.diagram').append('<a href="'+url+'">File</a>');
    $('.uploadedfiles').remove();
});
操纵:

检查这个