我编写了以下脚本来更改选中时复选框的背景颜色,但这在 Mozila 上不起作用
<script type="text/javascript">
function checkBoxList1OnCheck(listControlRef)
{
var inputItemArray = listControlRef.getElementsByTagName('input');
for (var i=0; i<inputItemArray.length; i++)
{
var inputItem = inputItemArray[i];
if ( inputItem.checked )
{
inputItem.parentElement.style.backgroundColor = 'Red';
}
else
{
inputItem.parentElement.style.backgroundColor = 'White';
}
}
}
</script>
<form id="form1" runat="server">
<div>
<asp:CheckBoxList id="CheckBoxList1" onclick="checkBoxList1OnCheck(this);" runat="server">
<asp:listitem value="1">Item 1</asp:listitem>
<asp:listitem value="2">Item 2</asp:listitem>
<asp:listitem value="3">Item 3</asp:listitem>
</asp:CheckBoxList>
</div>
</form>
甚至在页面加载时我添加如下
CheckBoxList1.Attributes.Add("onclick", "checkBoxList1OnCheck(this);");
但我仍然无法在 Mozila 中解决这个问题,有人可以帮助我吗
最佳答案
提示:查看 JavaScript 错误控制台 (CTRL + SHIFT + j),Mozilla 浏览器不支持 parentElement
。
尝试使用 parentNode
代替:
for (var i = 0; i < inputItemArray.length; i++) {
var inputItem = inputItemArray[i];
if (inputItem.checked) {
//inputItem.parentElement.style.backgroundColor = 'Red';//Won't work in Mozilla
inputItem.parentNode.style.backgroundColor = 'Red';
}
else {
//inputItem.parentElement.style.backgroundColor = 'White';//Won't work in Mozilla
inputItem.parentNode.style.backgroundColor = 'White';
}
关于javascript - 为什么我的 javascript 在 Firefox 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6424976/