【问题标题】:how to get an svg from one div element to another div element如何从一个 div 元素获取 svg 到另一个 div 元素
【发布时间】:2021-09-04 01:39:11
【问题描述】:

我一直在尝试附加 SVG 作为附加其他 HTML 元素,但除了 SVG 元素,所有其他 HTML 元素都附加。

以下是我试过的

例如,我的原始 SVG 集是为 div 动态创建的

HTML:

<div id="div3">
    <svg width="100" height="100">
        <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
    </svg>
    <svg height="140" width="500">
        <ellipse cx="200" cy="80" rx="100" ry="50" style="fill:yellow;stroke:purple;stroke-width:2" />
    </svg>
    <svg width="400" height="110">
        <rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)" />
    </svg>
    <svg height="100" width="500">
        <ellipse cx="240" cy="50" rx="220" ry="30" style="fill:yellow" />
        <ellipse cx="220" cy="50" rx="190" ry="20" style="fill:white" />
    </svg>
</div>
<div id='slides'></div>

JavaScript:

var svgsiblings = $('#div3').children().siblings("svg");
var slides = document.getElementById( 'items2' );
var theDiv ="";
for (let i = 1; i < 10; i++) {
    if (currentcheck , currenttype,i) {
        var itemid = "check" + i;
        theDiv  += "<div class='item"
        theDiv  += " ' id='"
        theDiv  += currentcheck+'-'+itemid
        theDiv  += "'> "
        theDiv  += svgsiblings[i-1].outerHTML 
        theDiv  += " </div>";
    }       
}                                 
$('#items2').append(theDiv);

通过上面的JavaScript,我尝试追加。但它只在需要 SVG 的地方添加了我在下面提到的 HTML 并带有一个空格

id 为 #div3 的 div 标签可能包含 10 个不同的 SVG。这样我就可以通过一个最多 10 个的 for 循环来循环它,

输出:

<div id='slides'>
     <div class='item' id='check1'>   </div>
     <div class='item' id='check2'>   </div>
     <div class='item' id='check3'>   </div>
     <div class='item' id='check4'>   </div>
     <div class='item' id='check5'>   </div>         
</div>

预期输出:

 <div id='slides'>
    ​<div class='item' id='check1'> <svg><circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" /></svg> </div>
    ​<div class='item' id='check2'> <svg><rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)" /></svg> </div>
    ​<div class='item' id='check3'> <svg><ellipse cx="200" cy="80" rx="100" ry="50" style="fill:yellow;stroke:purple;stroke-width:2" /></svg> </div>
    ​<div class='item' id='check4'> <svg><polygon points="100,10 40,198 190,78 10,78 160,198" style="fill:lime;stroke:purple;stroke-width:5;fill-rule:nonzero;"/></svg> </div>
    ​<div class='item' id='check5'> <svg><ellipse cx="240" cy="50" rx="220" ry="30" style="fill:yellow" /><ellipse cx="220" cy="50" rx="190" ry="20" style="fill:white" /></svg> </div>
 </div>

【问题讨论】:

    标签: javascript html jquery svg append


    【解决方案1】:

    您将 jQuery 与 vanilla JS 混合在一起,虽然这不是一件坏事,但它可能会令人困惑。使用其中一种对可读性(和最佳实践)有好处。

    $('#div3 svg').each(
       function(i) {
          $('#slides').append(
            $('<div>').append($(this))
                      .addClass('item')
                      .attr('id', `check${i+1}`))
       })
    

    这将遍历#div3 中的svg 元素,使用$.append() 作为创建div 元素的部分的包装器,并将$(this)(svg)插入到新的div 中。在该包装器之外,它应用了 className 和一个 id。

    移动 svg 从它的原始位置到幻灯片。如果您想将其复制到幻灯片上,您可以进行以下小改动:

    ...
    $('<div>').append($(this).clone())
    ...
    

    // and here's that in one line
    $('#div3 svg').each(function(i) { $('#slides').append($('<div>').append($(this)).addClass('item').attr('id', `check${i+1}`))})
    .item {
      float: left;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="div3">
      <svg width="100" height="100">
            <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
        </svg>
      <svg height="140" width="500">
            <ellipse cx="200" cy="80" rx="100" ry="50" style="fill:yellow;stroke:purple;stroke-width:2" />
        </svg>
      <svg width="400" height="110">
            <rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)" />
        </svg>
      <svg height="100" width="500">
            <ellipse cx="240" cy="50" rx="220" ry="30" style="fill:yellow" />
            <ellipse cx="220" cy="50" rx="190" ry="20" style="fill:white" />
        </svg>
    </div>
    <div id='slides'></div>

    【讨论】:

    • 我有 10 个不同的 SVG,这就是为什么我使用 for 循环 10。这将有助于我在每个“项目”类中获取每 10 个 SVG
    • 是的,您能否更新您的问题以明确您需要什么,我会更新答案。也许举一个svgs的例子。从你的评论我不太明白
    • 谢谢,它适用于普通的 div 元素,但它在引导弹出窗口中不起作用,有什么原因吗
    • 那么,您的页面上有 SVG,当弹出框(这是一个模态框吗?)出现时,您想将它们复制到弹出框吗?这是引导程序 4 还是 5?
    猜你喜欢
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多