遇到JavaScript Toggle功能问题

时间:2014-10-21 02:09:41

标签: javascript

我的toggleElement函数存在问题,它不会将样式更改为阻止,然后不会更改。我不知道我是否将函数编码错误或者我是否正确调用它。

任何反馈都会很棒!!

由于

    <link href="chat.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery-1.3.2.js" ></script> 
<script type="text/javascript" src="chat.js" ></script> 
<title>Chat Room | BetterGamerzUnited</title>
<script>
function toggleElement(x){
  var x = document.getElementById(x);
  if(x.style.display == 'block'){
    x.style.display = 'none';
  }else{
    x.style.display = 'block';
  }
}
</script>
</head>
  <?php include $path . '/includes/header.php'; ?>
    <div id="colorpickerB" style='display:none;'>
      <div id="colorpicker">
        <img src="/members/chat/palette.png" id="palette" alt="Color Palette" border="1"/>
        <br />
        <input id="color" type="hidden" readonly="true" value="#000000" />
      </div>    
    </div>
    <table id="content">
      <tr id="chat-top">
        <td>
          <div id="scroll">
          </div>
        </td>
      </tr>
      <tr id="chat-btm">
        <td colspan="2">
          <div id="items">
            <input type="text" class="" id="messageBox" placeholder="Enter your message here" maxlength="2000" size="30" />
          </div>
          <div id="color">
            <button type="button" id="sampleText" class="chat-btn" onclick="toggleElement(colorpickerB);">
              Color
            </button>
          </div>          
          <input type="hidden" class="" id="userName" value="<?php echo $log_username; ?>" maxlength="20"/>
          <input type="button" class="chat-btn" value="Send" id="send" />
          <?php
          if ($user_ok === true) {
              if (($log_userlevel == '7') || ($log_userlevel == '8')) {
                echo '<input type="button" class="chat-btn" value="Delete All" id="delete" />';
              }
            }
          ?>
        </td>
      </tr>
    </table>
  </div>
<footer></footer>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

在致电colorpickerB之前,您似乎没有定义toggleElement(colorpickerB);。也许您打算传入字符串'colorpickerB'而不是标识符:

toggleElement('colorpickerB');