【问题标题】:How to append anchor tag from one place to another? [duplicate]如何将锚标签从一个地方附加到另一个地方? [复制]
【发布时间】:2014-07-29 00:56:39
【问题描述】:

我写成

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

现在我希望将带有类上传文件的锚标记附加到 带有类图的 div 由 jQuery 输出应该是

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

【问题讨论】:

    标签: javascript jquery jquery-ui


    【解决方案1】:

    或者干脆

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

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

    $('.uploadedfiles').each(function(){
        $(this).appendTo('.diagram').removeAttr('class')
    })
    

    【讨论】:

      【解决方案2】:
      document.getElementsByClassName("diagram")[0].innerHTML+='<a href="www.google.com">File</a>';
      

      【讨论】:

        【解决方案3】:

        我会向你推荐这样的东西:

        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”类的节点并将其添加到“图表”节点中。

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

        【讨论】:

          【解决方案4】:

          很简单,您只需为每个锚点启动单词上传器进行搜索。

          以下是代码:

          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/

          谢谢, 阿肖克

          【讨论】:

          • “起始词上传器”是什么意思? attribute starts with 选择器?为什么要使用这样的选择器?
          • 在这种情况下属性以上传者类开头。
          【解决方案5】:

          好的..

          你要这样的东西吗?

          <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();
          
          });
          

          小提琴:

          Check this

          【讨论】:

          猜你喜欢
          • 2023-03-06
          • 2021-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-12
          • 1970-01-01
          • 1970-01-01
          • 2013-01-18
          相关资源
          最近更新 更多