我无法使下列代码正常工作。我试图在JS中动态生成以下SVG:
<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>我试过:
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);发布于 2022-11-20 17:30:54
正如@enxaneta:注释的那样,您的代码有错误:
用于创建所需的svg元素
createElementNS()而不是createElement()
此外,this.#form不是一个有效的选择器。
而是使用这样的东西:
let form = document.getElementById('form');
form.appendChild(closeButton);
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);*{
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;
}<form id="form" class="form" action="">
<input type="text" placeholder="name">
<button id="btnClose" class="btnClose" type="button"></button>
</form>
我强烈建议使用更多的语义变量名称:
因为您的图标的父svg不是实际的按钮-您最好使用一个自我解释的名称,如"closeIconSvg“。
发布于 2022-11-20 13:55:08
为什么不用1行“懒散的方式”(除非有宽度、高度和路径的变量)?:)
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;<div id="button1">
</div>
<div id="button2">
</div>
发布于 2022-11-20 16:15:50

“
”艺术在旁观者的眼里,每个人都会有自己的诠释。
- E.A. Bucchianeri
<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>
https://stackoverflow.com/questions/74507771
复制相似问题