javascript - 使用 jQuery 创建和访问 SVG 标签?

标签 javascript jquery svg

<分区>

是否可以像这样在 jQuery 中创建 SVG 标签:

var dragSVG = $('<svg xmlns="http://www.w3.org/2000/svg"></svg>');
dragSVG.append('<rect x="0" y="0" width="20" height="20" style="fill:red"></rect>');

如果是这样,如何访问 DOM? IE。如果是 HTML,我会执行以下操作:

return dragSVG.html();

但是因为它不是 HTML,所以会抛出一个异常……或者我错过了一些完全基本的东西!?

编辑:

我会尝试更清楚地解释我要实现的目标;我有一个代表 SVG“项目”的按钮,可以将其拖到主 SVG Canvas 上。当用户开始拖动时,我想在鼠标下方显示 SVG“项目”以提供用户反馈。当用户将它放到 Canvas 上时,我需要将“项目”移动到主 Canvas 上。

      $('#testBtnDrag').draggable({
          opacity: 0.7,
          revert: 'invalid',
          cursorAt: { top: 0, left: 0},
          helper: function (event) {
              var dragSVG = '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20"><rect x="0" y="0" width="20" height="20" style="fill:red"></rect></svg>';
              return dragSVG;
          }              
      });

      // I can't attach the droppable to the SVG tag directly, IE / FF don't work with this
      // so we have to attach it to a <div> tag that wraps the <svg>.
      $('#drawArea').droppable({
        accept: '.svg-item',
        drop: function (event, ui) {
          // Get the mouse offset relative to the <svg> canvas
          var posX = event.originalEvent.clientX - $(this).offset().left;
          var posY = event.originalEvent.clientY - $(this).offset().top;

          // Get the dragged element and put it onto the "main" canvas
          var rawSVG = ui.helper.children().html()  // This won't work!
          var mainCanvas = $('#drawArea > svg');
          mainCanvas.append(rawSVG);
        }
      });

  });

最佳答案

A <svg> , <path> 等是 SVGAnimatedString 对象,jQuery 本身不能产生它们,因为正在使用 createElement标签创建功能。 jQuery 的简单 hack 看起来像:

enter image description here

    // line 526 in jquery-X.X.X.js, in my case it is jquery-1.9.1.js   
    // Single tag
    if ( parsed ) {
        // ----------------------------------------------------
        var xml_html_element;
        if ( parsed[1] == "svg" || parsed[1] == "path" ) {
            xml_html_element = context.createElementNS( "http://www.w3.org/2000/svg", parsed[1] );
        } else {
            xml_html_element = context.createElement( parsed[1] );
        }
        return [xml_html_element];
        // ----------------------------------------------------
        //return [ context.createElement( parsed[1] ) ];

    }

现在,您可以像这样使用 jQuery:

var $svg = $("<svg>").attr({'version': "1.1"});
var $path = $("<path>").attr({
    'd': 'M' + 10 + ',' + 100 + ' C' + 200 + ',' + 150 + ' ' + 200 + ',' + 170 + ' ' + 300 + ',' + 250,
    'fill': "none",
    'stroke': "rgba(100,100,100,0.9)",
    'stroke-width': "1"
});
var $body = $("body");
$body.append($svg.append($path));

当然,为了您的个人需要,您可以在这个 hack 中扩展 SVG 标签的数量:

parsed[1] == "svg" || parsed[1] == "path" || parsed[1] == "line" || etc. ...   

关于javascript - 使用 jQuery 创建和访问 SVG 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11792754/

相关文章:

javascript - 如何循环查询以从类中获取所有数据

javascript - Bootstrap Popover - 显示在底部

javascript - 在 Firefox 中隐藏 &lt;input type ="date"> 的日历弹出窗口

javascript - Angular 2 : Event not firing

javascript - 在浏览器关闭之前卸载 - 如何查找用户是否选择了 "Stay"或 "Leave"

python - 从数据库循环 svg 矩形

jquery - 如何设置 jqGrid 单一搜索字段的默认值

javascript - 尝试创建 jquery mouseover 和 mouseout 事件

html - div 的自动高度不符合其 SVG 图标子项的高度

php - PHP 中的 SVG 到 PNG 结果错误