2011-06-21 7 views
1

Я написал следующий сценарий, чтобы изменить задний цвет флажка при проверке, но это не работает на MozilaПочему мой браузер не работает в 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 console (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

Смежные вопросы