1. It works in IE, but Firefox does not work
IE version
<script type="text/javascript">
function checkALL(str)//JS selected for all control
{
var a=(str);
var n=;
for(var i=0;i<n;i++)
{
a[i].checked=;
}
}
</script>
Firefox version
<script type="text/javascript">
function checkALL(str)//JS selected for all control
{
var a=(str);
var n=;
for(var i=0;i<n;i++){
a[i].checked=("all").checked;
}
}
</script>
Cause analysis: It can only run under IE, so js run under Firefox does not work. In the Firefox version, the checkbox selected state with "id=all" is directly obtained, and then assigned to each item in the checkbox group with "name=str", so that the selected state can be kept synchronized.
2. The difference between () and ()
The above section js obtains the checkbox state in two ways. Judging from the name, their function should be similar, one obtains the element through id and the other obtains the element through name. However, there are differences between these two methods. If you do not pay attention during use, you may feel that you can use it in conjunction, which will cause trouble. At that time, I thought it would be good to use one, but after changing the name, the js code did not work. In fact, it was because I didn't understand it, which led to the wrong use.
(1)() accesses a specific element through id, because id is unique in a page, so this function returns an Element
(2)() accesses elements through name, because name is not unique in a page and can be renamed, so this function returns a set of Elements
It is precisely because one is an element and the other is an array, if you don’t pay attention to it when mixing it, you will make an error, resulting in the js not being able to run. For example, when I changed a[i].checked=("all").checked; to a[i].checked=("all").checked;, js run does not work (because js errors but does not report an error, so it feels that it does not work). In fact, it is not that it is not recognized here, because it is used incorrectly when calling. The correct way is to write a[i].checked=("all")[0].checked; after this, the effect is the same. Because there is only a checkbox with "name=all" in our page, we use [0] to take the first element in Elements, which is the element we obtain through a[i].checked=("all").checked;.
IE version
Copy the codeThe code is as follows:
<script type="text/javascript">
function checkALL(str)//JS selected for all control
{
var a=(str);
var n=;
for(var i=0;i<n;i++)
{
a[i].checked=;
}
}
</script>
Firefox version
Copy the codeThe code is as follows:
<script type="text/javascript">
function checkALL(str)//JS selected for all control
{
var a=(str);
var n=;
for(var i=0;i<n;i++){
a[i].checked=("all").checked;
}
}
</script>
Cause analysis: It can only run under IE, so js run under Firefox does not work. In the Firefox version, the checkbox selected state with "id=all" is directly obtained, and then assigned to each item in the checkbox group with "name=str", so that the selected state can be kept synchronized.
2. The difference between () and ()
The above section js obtains the checkbox state in two ways. Judging from the name, their function should be similar, one obtains the element through id and the other obtains the element through name. However, there are differences between these two methods. If you do not pay attention during use, you may feel that you can use it in conjunction, which will cause trouble. At that time, I thought it would be good to use one, but after changing the name, the js code did not work. In fact, it was because I didn't understand it, which led to the wrong use.
(1)() accesses a specific element through id, because id is unique in a page, so this function returns an Element
(2)() accesses elements through name, because name is not unique in a page and can be renamed, so this function returns a set of Elements
It is precisely because one is an element and the other is an array, if you don’t pay attention to it when mixing it, you will make an error, resulting in the js not being able to run. For example, when I changed a[i].checked=("all").checked; to a[i].checked=("all").checked;, js run does not work (because js errors but does not report an error, so it feels that it does not work). In fact, it is not that it is not recognized here, because it is used incorrectly when calling. The correct way is to write a[i].checked=("all")[0].checked; after this, the effect is the same. Because there is only a checkbox with "name=all" in our page, we use [0] to take the first element in Elements, which is the element we obtain through a[i].checked=("all").checked;.