DOMParser & SVG
DOMParser & SVG
js parse html to dom
https://developer.mozilla.org/zh-CN/docs/Web/API/DOMParser
https://davidwalsh.name/convert-html-stings-dom-nodes
https://alligator.io/js/domparser/
https://stackoverflow.com/questions/3103962/converting-html-string-into-dom-elements
Convert String to DOM Nodes
DOMParser().parseFromString
let doc = new DOMParser().parseFromString('<div><b>Hello!</b></div>', 'text/html');
let div = doc.body.firstChild;
let divs = doc.body.querySelectorAll('div');
document.createRange().createContextualFragment
let frag = document.createRange().createContextualFragment('<div>One</div><div>Two</div>');
console.log(frag);
/*
#document-fragment
<div>One</div>
<div>Two</div>
*/
let firstChild = frag.firstChild;
let firstDiv = frag.querySelector('div');
let allDivs = frag.querySelectorAll('div');
// fragement
// Use a DocumentFragment to store and then mass inject a list of DOM nodes
var frag = document.createDocumentFragment();
for(var x = 0; x < 10; x++) {
var li = document.createElement("li");
li.innerHTML = "List item " + x;
frag.appendChild(li);
}
namespace bug
<?xml version="1.0" encoding="UTF-8"?>
let str = `
<?xml version="1.0" encoding="UTF-8"?>
<svg width="847px" height="400px" viewBox="0 0 847 400" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Generator: Sketch 61.2 (89653) - https://sketch.com -->
<title>Slice 1</title>
<desc>Created with Sketch.</desc>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<rect id="Rectangle" stroke="#979797" fill="#FBD5D5" x="103.5" y="0.5" width="632" height="147"></rect>
<text id="舞台" font-family="PingFangSC-Medium, PingFang SC" font-size="18" font-weight="400" fill="#000000">
<tspan x="402" y="81">舞台</tspan>
</text>
<rect id="Rectangle" stroke="#979797" fill="#5461F2" x="0.5" y="188.5" width="102" height="211"></rect>
<text id="A区" font-family="PingFangSC-Medium, PingFang SC" font-size="18" font-weight="400" fill="#000000">
<tspan x="36" y="301">A区</tspan>
</text>
<rect id="Rectangle" stroke="#979797" fill="#84FFEF" x="170.5" y="192.5" width="498" height="152"></rect>
<text id="B区" font-family="PingFangSC-Medium, PingFang SC" font-size="18" font-weight="400" fill="#000000">
<tspan x="405" y="275">B区</tspan>
</text>
<rect id="Rectangle" stroke="#979797" fill="#A25AF1" x="738.5" y="183.5" width="108" height="216"></rect>
<text id="C区" font-family="PingFangSC-Medium, PingFang SC" font-size="18" font-weight="400" fill="#000000">
<tspan x="787" y="294">C区</tspan>
</text>
</g>
</svg>
`;
let svgDom = new DOMParser().parseFromString(str, `image/svg+xml`);

OK
let str = `
<svg width="847px" height="400px" viewBox="0 0 847 400" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Generator: Sketch 61.2 (89653) - https://sketch.com -->
<title>Slice 1</title>
<desc>Created with Sketch.</desc>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<rect id="Rectangle" stroke="#979797" fill="#FBD5D5" x="103.5" y="0.5" width="632" height="147"></rect>
<text id="舞台" font-family="PingFangSC-Medium, PingFang SC" font-size="18" font-weight="400" fill="#000000">
<tspan x="402" y="81">舞台</tspan>
</text>
<rect id="Rectangle" stroke="#979797" fill="#5461F2" x="0.5" y="188.5" width="102" height="211"></rect>
<text id="A区" font-family="PingFangSC-Medium, PingFang SC" font-size="18" font-weight="400" fill="#000000">
<tspan x="36" y="301">A区</tspan>
</text>
<rect id="Rectangle" stroke="#979797" fill="#84FFEF" x="170.5" y="192.5" width="498" height="152"></rect>
<text id="B区" font-family="PingFangSC-Medium, PingFang SC" font-size="18" font-weight="400" fill="#000000">
<tspan x="405" y="275">B区</tspan>
</text>
<rect id="Rectangle" stroke="#979797" fill="#A25AF1" x="738.5" y="183.5" width="108" height="216"></rect>
<text id="C区" font-family="PingFangSC-Medium, PingFang SC" font-size="18" font-weight="400" fill="#000000">
<tspan x="787" y="294">C区</tspan>
</text>
</g>
</svg>
`;
let svgDom = new DOMParser().parseFromString(str, `image/svg+xml`);

DOMParser & SVG的更多相关文章
- d3 parse字符串形式的xml svg and append to element
参考这个方法,但不想修改d3 https://gist.github.com/biovisualize/373c6216b5634327099a 虽然也绕了点弯,但还算很快了,比较满意,也学到了,记下 ...
- 【Web动画】SVG 实现复杂线条动画
在上一篇文章中,我们初步实现了一些利用基本图形就能完成的线条动画: [Web动画]SVG 线条动画入门 当然,事物都是朝着熵增焓减的方向发展的,复杂线条也肯定比有序线条要多. 很多时候,我们无法人工去 ...
- 【Web动画】SVG 线条动画入门
通常我们说的 Web 动画,包含了三大类. CSS3 动画 javascript 动画(canvas) html 动画(SVG) 个人认为 3 种动画各有优劣,实际应用中根据掌握情况作出取舍,本文讨论 ...
- SVG:textPath深入理解
SVG的文本可以沿着一条自定义的Path来排布,比如曲线.圆形等等,使用方式如下所示(来源MDN): <svg viewBox="0 0 1000 300" xmlns=&q ...
- SVG:linearGradient渐变在直线上失效的问题解决方案
SVG开发里有个较为少见的问题. 对x1=x2或者y1=y2的直线(line以及path),比如: <path d="M200,10 200,100" stroke=&quo ...
- HTML5_05之SVG扩展、地理定位、拖放
1.SVG绘图总结: ①方法一:已有svg文件,<img src="x.svg"> 方法二:<body><svg></svg>&l ...
- HTML5_04之SVG绘图
1.关于Canvas绘制图像: 问题:需要绘制多张图片时,必须等待所有图片加载完成才能开始绘制:而每张图片都是异步请求,彼此没有先后顺序,哪一张先加载完成完全无法预测: 方案: var progres ...
- 关于SVG的viewBox
在SVG中,通过svg标记的 width和height可以规定这段SVG代码所表达的数据在绘制时所占用的空间大小 如下代码svg设置了宽度与高度,rect同样,所以结果自然是全屏 <svg wi ...
- JavaScript权威设计--jQuery,Ajax.animate,SVG(简要学习笔记二十)[完结篇]
1.$和jquery在全局命名空间中定义的唯一两个变量. 2.jquery是工厂函数,不是构造函数.他返回一个新创建的对象. 3.jquery的四种调用方式: <1>传递C ...
随机推荐
- 20201104gryz模拟赛解题报告
写在前面 \(Luckyblock\) 良心出题人, 题面好评 T1还是蛮简单的,用一个栈来维护就能过(某天听说 \(Luckyblock\) 出了套题,T1是个考栈的,看来就是这道了 注:栈的清空只 ...
- Spring boot 使用Redis 消息队列
package com.loan.msg.config; import com.loan.msg.service.MessageReceiver; import org.springframework ...
- k8s command & args
命令和参数说明: command.args两项实现覆盖Dockerfile中ENTRYPOINT的功能,具体的command命令代替ENTRYPOINT的命令行,args代表集体的参数. 如果comm ...
- go语言常见面试题
前言 从网上找了一些面试题,觉得有意思的我都记录下来,自己学习和大家一起学习吧. 有些初级的题目只放答案,一些值得探讨的问题我会写上我自己的分析过程,希望大家多多交流. 原文链接 选择题 1.[初级] ...
- 学会lambda表达式,能让你少敲1000行代码!
01.什么是 lambda 表达式 1. 函数式接口 在聊起 lambda 表达式之前,我们不得不提起函数式接口:一个接口只包含唯一的方法,那么它就是函数式接口.例如: public class La ...
- 小心 Enum Parse 中的坑
小心 Enum Parse 中的坑 Intro 最近使用枚举的时候,踩了一个小坑,分享一下,主要是枚举从 int 值转成枚举时可能会遇到 Sample 来看下面的示例: 首先定义一个枚举: publi ...
- 自己动手实现java断点/单步调试(二)
自从上一篇<自己动手实现java断点/单步调试(一)> 是时候应该总结一下JDI的事件了 事件类型 描述 ClassPrepareEvent 装载某个指定的类所引发的事件 ClassUnl ...
- unix环境高级编程第三章笔记
文件描述符 1.文件描述符的概念 对于内核而言,所有打开的文件都会用一个文件描述符来引用,打开或和创建一个新文件的时候,内核会给进程返回一个文件描述符,而当使用read write时,可以使用这个文件 ...
- trie浅谈
关于trie 其实字典树和以上两种算法有很大不同,但是hash由于其优秀的应用,导致有些字符串查找用hash也是可行的. 字典树中支持添加,查找,区间查询(可持久化字典树),而且在异或操作上有 ...
- OkHttp Client Ignore certificate
import okhttp3.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.net.ssl.*; i ...