繁体   English   中英

Jquery 或 HTML 问题? 为什么我的选择器不工作?

[英]Jquery or HTML problem? Why isn't my selector working?

当我单击“overview_table_header”class 时,以下 function 不会产生警报。 我究竟做错了什么?

<div class='overview_table_wrapper'> 
      <table> 
        <thead> 
          <tr> 
            <th class='col_1'> 
              <span class='overview_table_header' data-sort='DESC'> 
                Contest
              </span> 
              <span class='arrow'>></span> 
            </th> 
            <th class='col_2'> 
              <span class='overview_table_header' data-sort='DESC'> 
                Tweets
              </span> 
              <span class='arrow'></span> 
            </th> 
            <th class='col_3'> 
              <span class='overview_table_header' data-sort='DESC'> 
                Starts
              </span> 
              <span class='arrow'></span> 
            </th> 
            <th class='col_4'> 
              <span class='overview_table_header' data-sort='DESC'> 
                Ends
              </span> 
              <span class='arrow'></span> 
            </th> 
          </tr> 
        </thead> 

.js 文件:

(我正在加载上面未显示的此文件;其他 jquery 使用此文件)

$('.overview_table_header').click(function() {
alert("clicked!");
});

1) 确保 jQuery.js 包含在页面中(在下面的代码片段之前)。
2)在ready事件中添加点击绑定并添加Script标签

<script type="text/javascript">
 $(function(){
   $('.overview_table_header').click(function() { alert("clicked!"); });
 });
</script>

将此部分放在页面的 head 标签中

<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'></script>

<script>
$(document).ready(function(){
    $('.overview_table_header').click(function() {
        alert("clicked!");
    });
});
</script>

我不知道您的代码结构究竟如何,但这是一个有效的结构:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
    <title>jQuery Test</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <meta name="generator" content="Geany 0.20" />
    <script type="text/Javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    <script type="text/Javascript">
        $(document).ready(function(){
            $('.overview_table_header').click(function() {
                alert("clicked!");
            });
        });
    </script>
</head>

<body>
    <div class='overview_table_wrapper'> 
          <table> 
            <thead> 
              <tr> 
                <th class='col_1'> 
                  <span class='overview_table_header' data-sort='DESC'> 
                    Contest
                  </span> 
                  <span class='arrow'>></span> 
                </th> 
                <th class='col_2'> 
                  <span class='overview_table_header' data-sort='DESC'> 
                    Tweets
                  </span> 
                  <span class='arrow'></span> 
                </th> 
                <th class='col_3'> 
                  <span class='overview_table_header' data-sort='DESC'> 
                    Starts
                  </span> 
                  <span class='arrow'></span> 
                </th> 
                <th class='col_4'> 
                  <span class='overview_table_header' data-sort='DESC'> 
                    Ends
                  </span> 
                  <span class='arrow'></span> 
                </th> 
              </tr> 
            </thead> 
    </div>  
</body>

</html>

有了您提供给我们的内容,这应该可以正常工作,但我想也许这张表也是用 javascript 生成的? 如果是这样,您将需要使用委托方法将侦听器分配给这些跨度标签

<script>
$('.overview_table_wrapper').delegate('.overview_table_header','click', function() {
alert("clicked!");
});
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM