【发布时间】:2021-10-23 01:07:45
【问题描述】:
我正在考虑如何用纯JavaScript创建Material Icon(document.createElement)
const iconBox = document.createElement("div");
iconBox.style.width = "28px";
iconBox.style.cursor = "pointer";
iconBox.style.display = "flex";
iconBox.style.opacity = "1";
iconBox.style.position = "relative";
iconBox.style.alignItems = "center";
iconBox.style.justifyContent = "center";
iconBox.style.backgroundColor = "#ffffff";
const svg = document.createElement("svg");
svg.style.width = "1em";
svg.style.height = "1em";
svg.style.flexShrink = "inlineBlock";
svg.style.transition = "fill 200ms";
svg.style.flexShrink = "0";
svg.style.userSelect = "none";
svg.style.fontSize = "2.1875rem";
const path = document.createElement("path");
path.setAttribute(
"d",
"M14.59 8L12 10.59 9.41 8 8 9.41 10.59 12 8 14.59 9.41 16 12 13.41 14.59 16 16 14.59 13.41 12 16 9.41 14.59 8zM12 2C6.47 2 2 6.47 2 12s4.47 10 10 10 10-4.47 10-10S17.53 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8z"
);
svg.appendChild(path);
iconBox.appendChild(svg);
const container = document.getElementById("container");
container!.appendChild(iconBox);
console.log(container, "container");
但不知何故,在网络浏览器中,图标不显示(div、svg 和 path 在那里)
这是网络浏览器控制台的屏幕截图。
这是我的codesandbox,整个代码都在那里。
【问题讨论】:
标签: html svg material-ui