2

如果更改下拉列表中的选定项目,我正在尝试更改表格单元格的背景颜色。我对文本框使用相同的 javascript,它工作正常。在萤火虫中,当从选择中调用时,“单元格”是未定义的。

这是我的脚本/html

<html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server">
  <title>Untitled Page</title>
   <script type="text/javascript">
    function changeText(cell, shown, hidden)
    {
     if (shown == hidden)
     {
      cell.style.backgroundColor="red";
     }
     else
     {
      cell.style.backgroundColor="green";
     }
    }
   </script>
  </head>
  <body>
   <form id="form1" runat="server">
    <table cellpadding="5">
     <tr>
      <td>
       Cell 1
      </td>
     <td>
      <select id="catBlah" OnChange="changeText(this.parentnode, this.options[this.selectedIndex].value, '789');">
       <option value=""></option>
       <option selected="selected" value="789">Item 1</option>
       <option value="000">Item 2</option>
       <option value="456">Item 3</option>
       <option value="123">Item 4</option>
      </select>
     </td>
    </tr>
    <tr>
     <td>
      <input type="text" value="blue" onchange="changeText(this.parentNode, this.value, 'blue');" />
     </td>
     <td>
      Cell 4
     </td>
    </tr>
   </table>
  </form>
 </body>
</html>

如果我只是传递选择对象(使用“this”而不是“this.parentnode”),我可以更改选择的背景颜色(可能符合要求),但我不知道如何获取父节点。

谢谢

4

2 回答 2

5

它是parentNode(注意外壳)

于 2011-06-28T13:31:39.683 回答
2

我相信您的代码正在运行(我可能已经通过摆弄它来修复它)。它看起来像是你的外壳。请参阅:http: //jsfiddle.net/kaleb/y6UuL/

看来您只是无法看到背景颜色。我在您的单元格中添加了填充。

于 2011-06-28T13:41:51.510 回答