我無法使以下代碼正常作業。我正在嘗試在 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);
uj5u.com熱心網友回復:
正如@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”。
uj5u.com熱心網友回復:
為什么不在 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>
uj5u.com熱心網友回復:

“藝術在旁觀者的眼中,每個人都會有自己的解讀。”
——EA 布基亞內里
<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://www.uj5u.com/qukuanlian/536429.html
