Recently wrote a project, used to innerHTML, but, found that after calling innerHTML, the onclick invalid, which is also expected, because innerHTML is inserted as a text button, so the onclick event is not recognized, We have to find another way out, we can not directly implement, but can be implemented indirectly, here is an example:
Example one: This example is not achievable by the onclick
Copy Code code as follows:
<script defer>
function Inscell (TH)
var name=th.name;
var id=th.id;
if (name== "no") {
Id.innerhtml= "<input type= ' button ' onclick= ' Inscall (this) ' value= ' online '
Name= ' no ' id= ' 1 ' > ';
}else{
Id.innerhtml= "<input type= ' button ' onclick= ' Inscall (this) ' value= ' offline '
Name= ' yes ' id= ' 1 ' > ';
}
</script>
<body>
<br/>
<span id= "MSG" >
<input type= "button" onclick= "Inscall (This)" value= "Offline" name= "yes" id= "1" >
</span>
</body>
The above example is, according to the button's name to determine whether the line, or offline ~ ~ ~ We found that when the first click, then click on the invalid ~~~~~~
At this time we can implement in disguise, as follows:
Copy Code code as follows:
<script defer>
function Inscell (TH)
{
var name=th.name;
var id=th.id;
Alert (name+ "--" +id);
var Span=document.getelementbyid (ID);
if (name== "no") {
Span.innerhtml= "<input type= ' button ' value= ' off-line ' >";
Span.name= "yes";
}else{
Span.innerhtml= "<input type=" button ' value= ' online ' > ';
Span.name= "No";
}
}
</script>
<body>
<br/>
<span id= "1" onclick= "Inscell (This)" Name= "No" >
<input type= "button" value= "Online" >
</span>
</body>