0

我正在尝试使用 java-script 隐藏单击按钮。我的代码由于某种原因无法正常工作,这是代码。一切正常屏幕上弹出警报但隐藏功能没有运气。

   <html>
   <head>
   <title>Hello JQuery world!</title>
   <script type="text/javascript" src="jquery-1.10.2.min.js"></script>
   <script type="text/javascript" src="script1.js"></script>
   </head>
   <body>
   <table class="data" id="celebs">
<thead>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Occupation</th>
        <th>Approx. Location</th>
        <th>Price</th>
    </tr>
</thead>
<tbody>
    <tr>
        <th>1</th>
        <th>Johnny</th>
        <th>Wrestler</th>
        <th>Knocksville</th>
        <th>500</th>
    </tr>
        <tr>
        <th>2</th>
        <th>Molly</th>
        <th>Pharmacist</th>
        <th>Germany</th>
        <th>1500</th>
    </tr>
    <tr>
        <th>3</th>
        <th>Scooby Doo</th>
        <th>Detective</th>
        <th>Jersey</th>
        <th>Scooby Snacks</th>
    </tr>
    <tr>
        <th>4</th>
        <th>Pacman</th>
        <th>Character</th>
        <th>Area 51</th>
        <th>20</th>
    </tr>
    <tr>
        <th>5</th>
        <th>Ryu</th>
        <th>Street Fighter</th>
        <th>Japan</th>
        <th>1891564</th>
     </tr>
      </tbody>

      </table>
      <input type="button" id="hideButton" value="hide" />
      <input type="button" id="disclaimer" value="hide" />
      </body>
     </html>

java脚本如下

       $(function(){
        alert('Welcome');
         });

      $(function(){
 alert($('#celebs tbody tr:even').length + ' elements!');
      });
      $('#hideButton').click(function(){
$('#celebs').hide();
    });
    $(function(){
var fontSize = $('#celebs tbody tr:first').css('font-size');
alert(fontSize);
     });
   $(function(){
$('#celebs tbody tr:even').css('background-color', '#0C6');
    });
   $(function(){
$('#celebs tbody tr:odd').css({'background-color': '#930', 'color':'#C6F' }
    );
    });
4

1 回答 1

1

在加载文档之前调用您的事件绑定函数。所以你的事件绑定将不起作用。您应该将事件绑定函数放在(function(){..})块中。

$(function(){
        alert('Welcome');
        alert($('#celebs tbody tr:even').length + ' elements!');
        $('#hideButton').click(function(){
            $('#celebs').hide();
        });
        var fontSize = $('#celebs tbody tr:first').css('font-size');
        alert(fontSize);
        $('#celebs tbody tr:even').css('background-color', '#0C6');
        $('#celebs tbody tr:odd').css({'background-color': '#930', 'color':'#C6F' }
        );
    });
于 2013-11-07T23:31:49.753 回答