首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在JS中动态创建SVG路径

在JS中动态创建SVG路径
EN

Stack Overflow用户
提问于 2022-11-20 11:42:51
回答 3查看 52关注 0票数 0

我无法使下列代码正常工作。我试图在JS中动态生成以下SVG:

代码语言:javascript
复制
<svg xmlns="http://www.w3.org/2000/svg" height="48" width="48"><path d="m12.45 37.65-2.1-2.1L21.9 24 10.35 12.45l2.1-2.1L24 21.9l11.55-11.55 2.1 2.1L26.1 24l11.55 11.55-2.1 2.1L24 26.1Z"/></svg>

我试过:

代码语言:javascript
复制
let closeButton = document.createElement("svg");
closeButton.setAttribute("height", "48");
closeButton.setAttribute("width", "48");
let closeButtonPath = document.createElementNS('http://www.w3.org/2000/svg',"path"); 
closeButtonPath.setAttributeNS(null, "d", "m12.45 37.65-2.1-2.1L21.9 24 10.35 12.45l2.1-2.1L24 21.9l11.55-11.55 2.1 2.1L26.1 24l11.55 11.55-2.1 2.1L24 26.1Z");
closeButton.appendChild(closeButtonPath);
this.#form.appendChild(closeButton);
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-11-20 17:30:54

正如@enxaneta:注释的那样,您的代码有错误:

用于创建所需的svg元素

createElementNS()而不是createElement()

此外,this.#form不是一个有效的选择器。

而是使用这样的东西:

代码语言:javascript
复制
let form = document.getElementById('form');
form.appendChild(closeButton);

代码语言:javascript
复制
const ns ="http://www.w3.org/2000/svg";

let closeIconSvg = document.createElementNS(ns, "svg");
closeIconSvg.setAttribute("viewBox", "0 0 48 48");
closeIconSvg.classList.add('closeIconSvg');

let closeIconPath = document.createElementNS(ns,"path"); 
closeIconPath.setAttribute("d", "m12.45 37.65 -2.1-2.1L21.9 24 10.35 12.45l2.1-2.1L24 21.9l11.55-11.55 2.1 2.1L26.1 24l11.55 11.55-2.1 2.1L24 26.1Z");
closeIconSvg.appendChild(closeIconPath);

//let form = document.getElementById('form');
let closeButton = document.getElementById('btnClose');
closeButton.appendChild(closeIconSvg);
代码语言:javascript
复制
*{
  box-sizing:border-box
}

.form{
  font-size:48px;
  display:flex;
  align-items:center;
}

input, 
.btnClose{
  margin:0;
  padding:0.15em 0.3em 0.3em 0.3em;
  font-size:1em;
  border:1px solid #ccc;
  background:#fff;
}

.closeIconSvg{
  width:auto;
  height:1em;
  vertical-align: -0.25em;
}
代码语言:javascript
复制
<form id="form" class="form" action="">
  <input type="text" placeholder="name">
  <button  id="btnClose" class="btnClose" type="button"></button>
</form>

我强烈建议使用更多的语义变量名称:

因为您的图标的父svg不是实际的按钮-您最好使用一个自我解释的名称,如"closeIconSvg“。

票数 1
EN

Stack Overflow用户

发布于 2022-11-20 13:55:08

为什么不用1行“懒散的方式”(除非有宽度、高度和路径的变量)?:)

代码语言:javascript
复制
let svg_str = '<svg xmlns="http://www.w3.org/2000/svg" width="48px" height="48px"><path d="m12.45 37.65-2.1-2.1L21.9 24 10.35 12.45l2.1-2.1L24 21.9l11.55-11.55 2.1 2.1L26.1 24l11.55 11.55-2.1 2.1L24 26.1Z"/></svg>';
document.querySelector('#button1').innerHTML = svg_str;


// if height and widht has to be changed otherwise you put them directly in svg_str
const h = 48 + 'px';
const w = 48 + 'px';
// if path is a variable otherwise you put directly in svg_str
const path_d = "m12.45 37.65-2.1-2.1L21.9 24 10.35 12.45l2.1-2.1L24 21.9l11.55-11.55 2.1 2.1L26.1 24l11.55 11.55-2.1 2.1L24 26.1Z";

svg_str = '<svg xmlns="http://www.w3.org/2000/svg" width="' + w + '" height="' + h + '"><path d="' + path_d + '"/></svg>';
document.querySelector('#button2').innerHTML = svg_str;
代码语言:javascript
复制
<div id="button1">

</div>
<div id="button2">

</div>

票数 0
EN

Stack Overflow用户

发布于 2022-11-20 16:15:50

”艺术在旁观者的眼里,每个人都会有自己的诠释。

- E.A. Bucchianeri

代码语言:javascript
复制
<style>
  svg-icon { background:pink }
</style>

<svg-icon></svg-icon>
<svg-icon width="80"></svg-icon>
<svg-icon width="180"></svg-icon>

<script>
customElements.define("svg-icon", class extends HTMLElement{
  connectedCallback(){
    this.style.display = "inline-block";
    let width = (this.getAttribute("width") || 48) + "px";
    this.innerHTML =`<svg width="${width}" height="${width}" viewBox="0 0 48 44">`
                    + `<path d="m12.4 37.6-2.1-2.1 11.6-11.5-11.6-11.6 2.1-2.1 11.6 11.6 11.6-11.6 2.1 2.1-11.6 11.6 11.6 11.6-2.1 2.1-11.6-11.6z"/>`
                    + `</svg>`;
  }
});
</script>

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

https://stackoverflow.com/questions/74507771

复制
相关文章

相似问题

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