Diferència entre revisions de la pàgina «Solucio botons celestials»
De wikiserver
(Es crea la pàgina amb «HTML: <pre> <html> <head> <script type="text/javascript" src="bceles.js"></script> </head> <body></body> </html> </pre> Javascript: <pre> window…».) |
|||
Línia 1: | Línia 1: | ||
HTML: | HTML: | ||
− | < | + | <source lang="java"> |
<html> | <html> | ||
<head> | <head> | ||
Línia 8: | Línia 8: | ||
</html> | </html> | ||
− | </ | + | </source> |
Javascript: | Javascript: | ||
− | < | + | <source lang="java"> |
window.listId = []; | window.listId = []; | ||
window.numButton = -1; | window.numButton = -1; | ||
Línia 62: | Línia 62: | ||
window.onload = function(){init();}; | window.onload = function(){init();}; | ||
− | </ | + | </source> |
Revisió del 15:17, 5 feb 2015
HTML:
<html>
<head>
<script type="text/javascript" src="bceles.js"></script>
</head>
<body></body>
</html>
Javascript:
window.listId = [];
window.numButton = -1;
function init(){
createButton("func0");
createButton("func1");
}
function func0(){
//angelet
var maxb = Math.floor(Math.random()*10);
for(i=0;i<maxb;i++){
createButton();
}
}
function func1(){
//diablet
var maxelements = Math.min(20,window.listId.length);
var numbuttondeleted = Math.floor(Math.random()*maxelements);
for(i=0;i<numbuttondeleted;i++){
deleteNode();
}
function deleteNode(){
var index = Math.floor(Math.random()*window.listId.length);
var delboton = document.getElementById(window.listId[index]+"");
delboton.parentNode.removeChild(delboton);
window.listId.splice(index,1);
}
}
function createButton(func){
var div = document.createElement("span");
div.setAttribute("id", window.numButton)
var b = document.createElement("button");
func = (func)? func : "func"+Math.floor(Math.random()*2);
b.onclick = function(){eval(func +"()");};
var t = document.createTextNode("b" + window.numButton);
b.appendChild(t);
div.appendChild(b);
document.body.appendChild(div);
window.listId.push(window.numButton);
window.numButton++;
}
window.onload = function(){init();};