admin管理员组文章数量:1415420
I need the following button created in vanilla javascript:
<button class="etmkug-14 SuUwW">
<span class="etmkug-16 ctwFJG">Mark All Read</span>
</button>
Not sure about how to create and add the span:
var button = document.createElement('button');
button.setAttribute('class','etmkug-14 SuUwW');
button.setAttribute('style','margin-left: 10px;');
button.setAttribute('id','mark-all-read');
button.addEventListener('click', function(event){
.
.
.
}
var span = document.createElement('span');
span.setAttribute('class','etmkug-16 ctwFJG');
span.innerHTML = 'Mark All Read';
button.appendChild(span); // Is this how it's done????
I need the following button created in vanilla javascript:
<button class="etmkug-14 SuUwW">
<span class="etmkug-16 ctwFJG">Mark All Read</span>
</button>
Not sure about how to create and add the span:
var button = document.createElement('button');
button.setAttribute('class','etmkug-14 SuUwW');
button.setAttribute('style','margin-left: 10px;');
button.setAttribute('id','mark-all-read');
button.addEventListener('click', function(event){
.
.
.
}
var span = document.createElement('span');
span.setAttribute('class','etmkug-16 ctwFJG');
span.innerHTML = 'Mark All Read';
button.appendChild(span); // Is this how it's done????
Share
Improve this question
asked Jun 14, 2018 at 15:00
MB34MB34
4,45413 gold badges64 silver badges119 bronze badges
3
- 1 Looks right to me. Have you tested it? – Ryan Wilson Commented Jun 14, 2018 at 15:02
- You have working code that you just needed to try – j08691 Commented Jun 14, 2018 at 15:03
- Actually, haven't had time to test it, yet... – MB34 Commented Jun 14, 2018 at 15:23
3 Answers
Reset to default 4Yes that is how it is done. You just need to add this line document.body.append(button);
var button = document.createElement('button');
button.setAttribute('class','etmkug-14 SuUwW');
button.setAttribute('style','margin-left: 10px;');
button.setAttribute('id','mark-all-read');
var span = document.createElement('span');
span.setAttribute('class','etmkug-16 ctwFJG');
span.innerHTML = 'Mark All Read';
button.appendChild(span);
document.body.append(button);
If you're feeling lazy InnerHTML is your friend; Grab a container and append some HTML.
var container = document.getElementById('container');
container.innerHTML = container.innerHTML + '<button class="etmkug-14 SuUwW"><span class="etmkug-16 ctwFJG">Mark All Read</span></button>';
let newButton = document.createElement('button');
newButton.setAttribute("id", "myNewButton");
newButton.className = "myClassName";
newButton.innerText = "clickMe";
newButton.addEventListener("click", clickMe);
document.body.appendChild(newButton);
function clickMe(){
console.log("Hi! I am a new Button.");
}
本文标签: htmlCreate Button with vanilla JavaScriptStack Overflow
版权声明:本文标题:html - Create Button with vanilla JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745236096a2649047.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论