0

这是代码:html+js

<input type="submit" onclick="chk();" />
function chk(){
var buttonnode= document.createElement('input');
buttonnode.onClick = ddiv(buttonnode);
}

buttonnode.onclick 自动调用 ddiv 函数,但我希望它等待单击以继续执行该函数。怎么做?

4

2 回答 2

0

尝试如下...它会工作..

buttonnode.onclick = function (){ddiv(this);};
于 2013-02-07T15:11:54.553 回答
0

尝试如下...它将给出您需要的结果...

<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>
于 2013-02-07T16:53:17.093 回答