1. It works in IE, Firefox does not work
IE version
Copy code code as follows:
<script type = "text/javascript">
function checkall (str) // full selected JS
{{
var a = document.GetelementsByname (STR);
var n = a.Length;
for (var I = 0; i <n; i ++)
{{
a [i] .Checked = window.event.srcelement.checked;
}
}
</script>
Fire Fox version
Copy code code as follows:
<script type = "text/javascript">
function checkall (str) // full selected JS
{{
var a = document.GetelementsByname (STR);
var n = a.Length;
for (var I = 0; i <n; i ++) {
a [i] .Checked = document.GetelementByid ("all"). Checked;
}
}
</script>
Cause analysis: Window.event can only run under IE, so JS does not work under the Fire Fox. In the Fire Fox version, the checkbox of "ID = all" is directly selected, and then each item of the checkbox group of "name = str" is assigned to keep the selection status synchronization.
2. The difference between document.GetelementByid () and docume
The JS above obtains the state of Checkbox in two ways. From the name point of view, their role should be similar. One obtains elements through ID and one obtains elements through name. However, these two methods are different. If you don't pay attention during the use process, you may find it mixed, which will cause trouble. At that time, I felt like using one casually, but after the name was changed, the JS code did not work. In fact, it was because it was not understood, which caused the wrong use.
(1) Document.GetelementByid () is to access a certain element through ID, because the ID in one page is unique, so this function returns an element
(2) Document.GetelementsByname () is to access elements through name, because the name in one page is not the only one, it can be replaced, so this function returns a set of Elements
It is precisely because one is elements and the other is an array, so the error will be made when you don't pay attention when mixed, resulting in JS unable to run. For example, at the time I took a [i] .Checked = document.GetelementByid ("all"). Checked; changed to a [i] .check = document.getelementsbyname ("all"). Function (because JS is wrong but does not report errors, it feels like it does not work). In fact, it is not unsatisfactory here, because the correct writing method is a [i] .Checked = DOCUMENT.GetelementsByname ("All" "all ") [0] .Checked; after this change, the effect was the same. Because there is only one checkbox with "name = all" in our page, we use [0] to take the first element in Elements, that is, we use a [i] .Checked = Document.GetelementByid ("All") .Checked; the element obtained.