javascript - 为什么我的 javascript 在 Firefox 中不工作

标签 javascript asp.net

我编写了以下脚本来更改选中时复选框的背景颜色,但这在 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/

相关文章:

c# - 尝试使用 JSON.Net 使用 SmartyStreets JSON... "Cannot deserialize JSON array into type Components"

javascript - 遍历嵌套的 json 对象数组

Javascript:使用嵌套 for 循环将嵌套数组转换为单个数组

javascript - 使用 jquery on click 函数将特定字符附加到新的 div

javascript - 调用 Google People API 时出现 403 错误

asp.net - 在客户端验证摘要控件中显示有关自定义错误的自定义消息?

javascript - 如何使用 JavaScript 将 JSON 字符串中的值提取到列表项中

asp.net - 从字符串 "0.##"到类型 'Integer' 的转换无效

c# - 如何对多个实例使用 Using 语句?

c# - 将评论框脚本编辑为动态