这是代码:html+js
<input type="submit" onclick="chk();" />
function chk(){
var buttonnode= document.createElement('input');
buttonnode.onClick = ddiv(buttonnode);
}
buttonnode.onclick 自动调用 ddiv 函数,但我希望它等待单击以继续执行该函数。怎么做?
这是代码:html+js
<input type="submit" onclick="chk();" />
function chk(){
var buttonnode= document.createElement('input');
buttonnode.onClick = ddiv(buttonnode);
}
buttonnode.onclick 自动调用 ddiv 函数,但我希望它等待单击以继续执行该函数。怎么做?
尝试如下...它会工作..
buttonnode.onclick = function (){ddiv(this);};
尝试如下...它将给出您需要的结果...
<html>
<body>
<style>
.divstyle
{
border: 1px solid red;
}
</style>
<script>
function myFunction()
{
var txt = document.getElementById("txtinfo");
//div Creation
var myDiv = document.createElement("div");
myDiv.setAttribute("class", "divstyle");
myDiv.innerHTML = "<br>" + txt.value + "<br>";
//Button Creation
var myButton = document.createElement("input");
myButton.type = "button";
myButton.value = "my button";
myDiv.appendChild(myButton);
document.body.appendChild(myDiv);
myButton.onclick = function (){ddiv(this,myDiv);};
}
function ddiv(btn, div)
{
div.parentNode.removeChild(div);
}
</script>
<button onclick="myFunction()">Try it</button>
<input type="text" value="Click the Below Button" id="txtinfo"/>
</body>
</html>