var btn = document.getElementById('clickBtn');
btn.addEventListener('click', function(el) {
var btn1 = document.createElement("button");
var btn2 = document.createElement("button");
var span1 = document.createElement("span");
var span2 = document.createElement("span");
span1.appendChild(document.createTextNode("btn1"));
span2.appendChild(document.createTextNode("btn2"));
btn1.appendChild(span1);
btn2.appendChild(span2);
btn1.className = "btn green";
btn2.className = "btn orange";
btn1.type = "button";
btn2.type = "button";
btn.parentNode.appendChild(btn1);
btn.parentNode.appendChild(btn2);
btn.parentNode.removeChild(btn);
}, false)
.btn {
position: relative;
display: block;
margin: 30px auto;
padding: 0;
overflow: hidden;
border-width: 0;
outline: none;
border-radius: 2px;
box-shadow: 0 1px 4px rgba(0, 0, 0, .6);
background-color: #2ecc71;
color: #ecf0f1;
transition: background-color .3s;
}
.btn:hover, .btn:focus {
background-color: #27ae60;
}
.btn > * {
position: relative;
}
.btn span {
display: block;
padding: 12px 24px;
}
.btn:before {
content: "";
position: absolute;
top: 50%;
left: 50%;
display: block;
width: 0;
padding-top: 0;
border-radius: 100%;
background-color: rgba(236, 240, 241, .3);
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.btn:active:before {
width: 120%;
padding-top: 120%;
transition: width .2s ease-out, padding-top .2s ease-out;
}
/* Styles, not important */
*, *:before, *:after {
box-sizing: border-box;
}
html {
position: relative;
height: 100%;
}
body {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
background-color: #ecf0f1;
color: #34495e;
font-family: Trebuchet, Arial, sans-serif;
text-align: center;
}
h2 {
font-weight: normal;
}
.btn.orange {
background-color: #e67e22;
}
.btn.orange:hover, .btn.orange:focus {
background-color: #d35400;
}
.btn.red {
background-color: #e74c3c;
}
.btn.red:hover, .btn.red:focus {
background-color: #c0392b;
}
<div>
<button class="btn red" type="button" id="clickBtn"><span>Extra-long button to let you appreciate the effect.</span></button>
</div>
這是一個真正的「你能爲我這個代碼」請求。看看JavaScript的innerHTML改變你的DIV的內容,並嘗試自己編寫代碼,而不要求一個完整的解決方案。 http://www.w3schools.com/jsref/prop_html_innerhtml.asp – Bangkokian