首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >遍历JS中的所有SVG元素

问遍历JS中的所有SVG元素
EN

Stack Overflow用户
提问于 2019-08-19 11:50:17
回答 1查看 1.5K关注 0票数 0

我不能在JavaScript中查看SVG文件的所有子程序。我想要遍历所有的路径并对它们执行一个函数(将它们更改为多边形)。

我尝试过使用querySelectorAll("path");创建一系列路径,但没有成功。现在,我正在筛选SVG文件中的所有元素,并在进行路径转换。

​

代码语言:javascript
复制
<!DOCTYPE HTML>

<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Reader</title>
  </head>
  <body>
    <input type="file" id="fileReader" />
    <br>
    <p id="Content"></p>

    <script>
    document.getElementById("fileReader").addEventListener('change',function(){
      var fr = new FileReader();
      fr.onload = function(){
        console.log("File Loaded!")
      }
      parser = new DOMParser();
      var doc = parser.parseFromString(fr.readAsText(this.files[0]), "text/xml");
      console.log(doc);
      var path = "path";
      doc.querySelectorAll('*').forEach(function(){
          if($(this).is(path)){
              var polygon = doc.createElementNS("http://www.w3.org/2000/svg", "polygon");
              polygon.setAttribute("id", $(this).getAttribute("id"));
              console.log("Converting " + $(this).getAttribute("id"));
              var len = $(this).getTotalLength();
              var p = $(this).getPointAtLength(0);
              var seg = $(this).getPathSegAtLength(0);
              var stp=p.x+","+p.y;
              for(var i=1; i<len; i++){
                p=$(this).getPointAtLength(i);
                if ($(this).getPathSegAtLength(i)>seg) {
                  stp=stp+" "+p.x+","+p.y;
                  seg = $(this).getPathSegAtLength(i);
                }
              }
              polygon.setAttribute("points", stp);
              $(this).replaceWith(polygon);
          }
      });
    });
    </script>
  </body>
</html>

​

这给了我两个错误:

file:///C:/Users/Temp/Desktop/Experiment.html XML解析错误:语法错误位置:行编号1,列1:。

ReferenceError:$是未定义的。

我已经停止使用doc.children(),因为它不起作用。

EN

回答 1

Stack Overflow用户

发布于 2019-08-20 16:09:04

只需将jQuery库添加到项目中即可。

代码:

代码语言:javascript
复制
<!DOCTYPE HTML>

<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    <title>Reader</title>
  </head>
  <body>
    <input type="file" id="fileReader" />
    <br>
    <p id="Content"></p>

    <script>
    document.getElementById("fileReader").addEventListener('change',function(){
      var fr = new FileReader();
      fr.onload = function(){
        console.log("File Loaded!")
      }
      parser = new DOMParser();
      var doc = parser.parseFromString(fr.readAsText(this.files[0]), "text/xml");
      console.log(doc);
      var path = "path";
      doc.querySelectorAll('*').forEach(function(){
          if($(this).is(path)){
              var polygon = doc.createElementNS("http://www.w3.org/2000/svg", "polygon");
              polygon.setAttribute("id", $(this).getAttribute("id"));
              console.log("Converting " + $(this).getAttribute("id"));
              var len = $(this).getTotalLength();
              var p = $(this).getPointAtLength(0);
              var seg = $(this).getPathSegAtLength(0);
              var stp=p.x+","+p.y;
              for(var i=1; i<len; i++){
                p=$(this).getPointAtLength(i);
                if ($(this).getPathSegAtLength(i)>seg) {
                  stp=stp+" "+p.x+","+p.y;
                  seg = $(this).getPathSegAtLength(i);
                }
              }
              polygon.setAttribute("points", stp);
              $(this).replaceWith(polygon);
          }
      });
    });
    </script>
  </body>
</html>

​

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57556159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档