2011-06-21 34 views
1

工作,我写了下面的脚本检查时改变复选框的背景色,但是这是不工作的Mozila为什么我的JavaScript未在Firefox

<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任何一个可以帮助我

+1

究竟是不是工作?你会得到什么错误? –

+0

是不是在IE或Mozilla中工作? – rid

+0

我没有收到任何错误我无法得到我需要的输出 – Dotnet

回答

2

提示:看看JavaScript错误控制台(CTRL + SHIFT + j),parentElement在Mozilla浏览器中不受支持。

尝试使用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'; 

      } 
+1

非常感谢5arx它的工作 – Dotnet

+0

一个小问题,如果我想给一个代码而不是颜色我怎么能 – Dotnet

+0

我得到了很多谢谢 – Dotnet

相关问题