首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Javascript:如何创建没有文本内容的按钮元素

Javascript:如何创建没有文本内容的按钮元素
EN

Stack Overflow用户
提问于 2018-07-20 02:46:33
回答 4查看 323关注 0票数 0

在html中,我有一个不带文本的button元素。它有一个带有一些路径和矩形的svg子元素。它工作得很好:

我试着用javascript创建它。问题是,按钮是不可见的。如果我用textContentinnerHtml给它设置了一些文本,按钮可以看到文本,但是svg不在那里。如何在javascript中创建此按钮?代码如下:

代码语言:javascript
复制
var myButton = document.createElement("button");
myButton.setAttribute("class", "my-button");
myButton.setAttribute("id", "foo");

var mySVG = document.createElement("svg");
mySVG.setAttribute("id", "my-svg");
mySVG.setAttribute("viewBox", "0 0 12.25 15.45");

var icon1 = document.createElement("g");
icon1.setAttribute("class", "g-element1");
icon1.setAttribute("id", "g1");

var iconPath = document.createElement("path");
iconPath.setAttribute("d", "M0,25L0,0l12.25,7.7L0,15.45z");

var icon2 = document.createElement("g");
icon2.setAttribute("class", "g-element2");
icon2.setAttribute("id", "g2");

var rect1 = document.createElement("rect");
rect1.setAttribute("x", "0");
rect1.setAttribute("y", "0");
rect1.setAttribute("width", "4.1");
rect1.setAttribute("height", "15.45");

var rect2 = document.createElement("rect");
rect2.setAttribute("x", "8.1");
rect2.setAttribute("y", "0");
rect2.setAttribute("width", "4.1");
rect2.setAttribute("height", "15.45");

icon1.appendChild(iconPath);
icon2.appendChild(rect1);
icon2.appendChild(rect2);

mySVG.appendChild(icon1);
mySVG.appendChild(icon2);

myButton.appendChild(mySVG);

document.getElementById('some-element').appendChild(myButton)
代码语言:javascript
复制
.my-button {
  font-size: 14px;
  height: 17px;
  cursor: pointer;
  margin-left: 5px;
  &:hover, &:focus {
    opacity: .8;
  }
}
代码语言:javascript
复制
<div id="some-element">
<button class="my-button" id="foo">
    <svg id="my-svg" viewBox="0 0 12.25 15.45">
        <g class="g-element1" id="g1">
            <path d="M0,25L0,0l12.25,7.7L0,15.45z"/>
        </g>
        <g class="g-element2" id="g2">
            <rect x="0" y="0" width="4.1" height="15.45"/>
            <rect x="8.1" y="0" width="4.1" height="15.45"/>
         </g>
     </svg>
 </button>
 </div>

同样,当我在javascript中只创建按钮,并且没有设置文本(也没有svg )时,按钮是不可见的。

EN

回答 4

Stack Overflow用户

发布于 2018-07-20 03:19:46

SVG似乎在按钮内部折叠为零宽度和高度。您可以通过在其上设置显式的宽度和高度来防止这种情况:

代码语言:javascript
复制
.my-button {
  font-size: 14px;
  height: 17px;
  cursor: pointer;
  margin-left: 5px;
  &:hover, &:focus {
    opacity: .8;
  }
}
#my-svg {width: 100%; height: 100%}
代码语言:javascript
复制
<div id="some-element">
<button class="my-button" id="foo">
    <svg id="my-svg" viewBox="0 0 12.25 15.45">
        <g class="g-element1" id="g1">
            <path d="M0,25L0,0l12.25,7.7L0,15.45z"/>
        </g>
        <g class="g-element2" id="g2">
            <rect x="0" y="0" width="4.1" height="15.45"/>
            <rect x="8.1" y="0" width="4.1" height="15.45"/>
         </g>
     </svg>
 </button>
 </div>

无论SVG是内联定义的还是脚本生成的,这一点都应该适用。但请注意,在生成非HTML节点时,必须使用.createElementNS()并包含名称空间,如下所示:

代码语言:javascript
复制
var myButton = document.createElement("button");
myButton.setAttribute("class", "my-button");
myButton.setAttribute("id", "foo");

var svgns = "http://www.w3.org/2000/svg";

var mySVG = document.createElementNS(svgns, "svg");
mySVG.setAttribute("id", "my-svg");
mySVG.setAttribute("viewBox", "0 0 12.25 15.45");

var icon1 = document.createElementNS(svgns, "g");
icon1.setAttribute("class", "g-element1");
icon1.setAttribute("id", "g1");

var iconPath = document.createElementNS(svgns, "path");
iconPath.setAttribute("d", "M0,25L0,0l12.25,7.7L0,15.45z");

var icon2 = document.createElementNS(svgns, "g");
icon2.setAttribute("class", "g-element2");
icon2.setAttribute("id", "g2");

var rect1 = document.createElementNS(svgns, "rect");
rect1.setAttribute("x", "0");
rect1.setAttribute("y", "0");
rect1.setAttribute("width", "4.1");
rect1.setAttribute("height", "15.45");

var rect2 = document.createElementNS(svgns, "rect");
rect2.setAttribute("x", "8.1");
rect2.setAttribute("y", "0");
rect2.setAttribute("width", "4.1");
rect2.setAttribute("height", "15.45");

icon1.appendChild(iconPath);
icon2.appendChild(rect1);
icon2.appendChild(rect2);

mySVG.appendChild(icon1);
mySVG.appendChild(icon2);

document.getElementById('some-element').appendChild(mySVG)
代码语言:javascript
复制
#my-svg {
  width: 100%;
  height: 100%
}

button {height: 14px}
代码语言:javascript
复制
<button id="some-element"></div>

票数 1
EN

Stack Overflow用户

发布于 2018-07-20 03:53:51

在使用JavaScript创建SVG元素(包括SVG tage内的元素)时,需要使用带有适当名称空间URI http://www.w3.org/2000/svgdocument.createElementNS(namespaceURI, qualifiedName)。您还需要为SVG元素分配一个高度。

因为您必须对在SVG标记中创建的每个元素以及SVG标记本身使用命名空间,所以您可能希望使用curry函数来节省空间并防止输入错误:

代码语言:javascript
复制
const createSVGElement = qn => document.createElementNS("http://www.w3.org/2000/svg", qn);

以下是您的代码已修复:

代码语言:javascript
复制
var myButton = document.createElement("button");
myButton.setAttribute("class", "my-button");
myButton.setAttribute("id", "foo");

const createSVGElement = qn => document.createElementNS("http://www.w3.org/2000/svg", qn);

var mySVG = createSVGElement("svg");
mySVG.setAttribute("id", "my-svg");
mySVG.setAttribute('height', "14px");
mySVG.setAttribute("viewBox", "0 0 12.25 15.45");

var icon1 = createSVGElement("g");
icon1.setAttribute("class", "g-element1");
icon1.setAttribute("id", "g1");

var iconPath = createSVGElement("path");
iconPath.setAttribute("d", "M0,25L0,0l12.25,7.7L0,15.45z");

var icon2 = createSVGElement("g");
icon2.setAttribute("class", "g-element2");
icon2.setAttribute("id", "g2");

var rect1 = createSVGElement("rect");
rect1.setAttribute("x", "0");
rect1.setAttribute("y", "0");
rect1.setAttribute("width", "4.1");
rect1.setAttribute("height", "15.45");

var rect2 = createSVGElement("rect");
rect2.setAttribute("x", "8.1");
rect2.setAttribute("y", "0");
rect2.setAttribute("width", "4.1");
rect2.setAttribute("height", "15.45");

icon1.appendChild(iconPath);
icon2.appendChild(rect1);
icon2.appendChild(rect2);

mySVG.appendChild(icon1);
mySVG.appendChild(icon2);

myButton.appendChild(mySVG);

document.getElementById("some-element").appendChild(myButton);
代码语言:javascript
复制
.my-button {
  font-size: 14px;
  height: 17px;
  cursor: pointer;
  margin-left: 5px;
  &:hover, &:focus {
    opacity: .8;
  }
}
代码语言:javascript
复制
<div id="some-element">
<button class="my-button" id="foo">
    <svg id="my-svg" viewBox="0 0 12.25 15.45" height="14px">
        <g class="g-element1" id="g1">
            <path d="M0,25L0,0l12.25,7.7L0,15.45z"/>
        </g>
        <g class="g-element2" id="g2">
            <rect x="0" y="0" width="4.1" height="15.45"/>
            <rect x="8.1" y="0" width="4.1" height="15.45"/>
         </g>
     </svg>
 </button>
 </div>

票数 1
EN

Stack Overflow用户

发布于 2018-07-20 03:20:59

svg元素设置为button的innerHTML可能会给出结果。

代码语言:javascript
复制
document.getElementById('foo').innerHTML = '<svg id="my-svg" viewBox="0 0 12.25 15.45"><g class="g-element1" id="g1"><path d="M0,25L0,0l12.25,7.7L0,15.45z"/></g><g class="g-element2" id="g2"><rect x="0" y="0" width="4.1" height="15.45"/><rect x="8.1" y="0" width="4.1" height="15.45"/></g></svg>';
代码语言:javascript
复制
.my-button {
  font-size: 14px;
  height: 17px;
  cursor: pointer;
  margin-left: 5px;
  &:hover, &:focus {
    opacity: .8;
  }
}

svg
{
    height:100%;
  width:100%;
}
代码语言:javascript
复制
<div id="some-element">
<button class="my-button" id="foo">
 </button>
 </div>

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

https://stackoverflow.com/questions/51429864

复制
相关文章

相似问题

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