簡體   English   中英

遍歷JS中的所有SVG元素

[英]Iterate through all SVG elements in JS

我無法遍歷JavaScript中SVG文件的所有子級。 我想遍歷所有路徑並對它們執行功能(將它們更改為多邊形)。

我已經嘗試使用querySelectorAll("path");創建路徑數組querySelectorAll("path"); ,但無效。 現在,我正在嘗試篩選SVG文件中的所有元素,並隨心所欲地轉換路徑。

 <!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+","+py; for(var i=1; i<len; i++){ p=$(this).getPointAtLength(i); if ($(this).getPathSegAtLength(i)>seg) { stp=stp+" "+p.x+","+py; seg = $(this).getPathSegAtLength(i); } } polygon.setAttribute("points", stp); $(this).replaceWith(polygon); } }); }); </script> </body> </html> 

這給了我兩個錯誤:

XML分析錯誤:語法錯誤位置:file:/// C:/Users/Temp/Desktop/Experiment.html行號1,列1 :

ReferenceError:$未定義

由於它不起作用,我已停止嘗試使用doc.children()。

只需將jQuery庫添加到您的項目中即可。

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

碼:

 <!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+","+py; for(var i=1; i<len; i++){ p=$(this).getPointAtLength(i); if ($(this).getPathSegAtLength(i)>seg) { stp=stp+" "+p.x+","+py; seg = $(this).getPathSegAtLength(i); } } polygon.setAttribute("points", stp); $(this).replaceWith(polygon); } }); }); </script> </body> </html> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM