lendle wrote:
I have defined onclick event handler within the innerHTML property as
following:
var testButton1=oWi n.document.crea teElement("LABE L");
testButton1.inn erHTML="<input type='button' value='xx'
onclick=\"alert ('123')\">";
oLabel.appendCh ild(testButton1 );
the oLabel has already been append to document.body
You are creating invalid HTML. You are basically generating:
<label><input type='button' value='xx' onclick="alert( '123')"></label>
Which makes no sense at all. A <label> can't contain an <input>. You are
then appending that mess as the child of another label (oLabel), so you
are ending up with some HTML that probably looks something like:
<label><label>< input type='button' value='xx'
onclick="alert( '123')"></label></label>
It appears you want to create a button and put it on document.body. So do
that. Create the button and append it to document.body, NOT to a <label>:
<script type="text/javascript">
var testButton1 = document.create Element('input' );
testButton1.typ e = 'button';
testButton1.val ue = 'xx';
testButton1.onc lick = function() { alert('123'); };
document.body.a ppendChild(test Button1);
</script>
Tested and working in IE 6.0.2800 (pre-XPSP2) and 6.0.2900 (XPSP2),
Firefox 0.9.3 and Opera 7.54.
I'm guessing you took some code you did not understand that created a
<label>, set it's innerHTML and appended the <label> to the previous
element. Now you assume that everytime you create an element, it has to
be a <label>, you have to set it's innerHTML, and you have to append it
to the previous element.
--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ -
http://jibbering.com/faq