【问题标题】:click event on an image inside a span is not working跨度内图像上的单击事件不起作用
【发布时间】:2012-11-23 05:02:22
【问题描述】:

我可以将图像放在一个跨度内,并使用 jQuery 为该跨度提供点击事件。在这里它在我的代码中不起作用有任何想法。

<span id="removeNewTime" class="removeTime">
    <img src="Images/close button.png" alt="close" />
</span>

jQuery 代码

<script>
    $(document).ready(function(){
        $(".removeTime").click(function(){
            alert("hello");
        });
    });
</script>

【问题讨论】:

  • 错误信息是什么?
  • 检查 jquery 是否包含正确,将 alert 放入 ready

标签: jquery html


【解决方案1】:

这是您可能需要include jQuery 的工作,这篇文章告诉您如何add jQuery on page。

Live Demo

$(document).ready(function(){
    $(".removeTime").click(function(){
     alert("hello");
    });
});​

【讨论】:

    【解决方案2】:

    将 span 的 CSS 设置为 display:block; 或将 span 替换为 &lt;div&gt;

    【讨论】:

      【解决方案3】:

      确保在应用任何文档就绪函数之前包含 jquery 库:

      看看工作中的小提琴:

      http://jsfiddle.net/87Laj/

      <script src='https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js' type='text/javascript'></script>
      
      <script>
         $(document).ready(function(){
           $(".removeTime").click(function(){
            alert("hello");
          });
        });
      </script>
      

      【讨论】:

        【解决方案4】:

        确保您已将 jquery 包含在文件的 head 部分中。由于代码运行良好。

        为简单起见,请始终使用&lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;

        这将确保您始终拥有最新的 jquery 文件。

        【讨论】:

          【解决方案5】:

          可能是 jquery 文件中的问题,这是 jquery 代码工作所必需的。

          检查工作jsfiddle

          对js代码的小改进:

          $(function() {
              $(".removeTime").on('click', function(){
                   alert("hello");
              });
          });​
          

          【讨论】:

            【解决方案6】:

            此代码有效 注意代码中的一些要点

            1) 不要忘记添加最新的 jquery 文件,例如 http://code.jquery.com/jquery-1.8.3.min.js 给你的代码。

            2) 从http://jquery.com/download/下载

            【讨论】:

              【解决方案7】:

              有时我确实喜欢它的dosnt工作,

              然后我试试这个(如果页面在之前动态变化)

              $(document).ready(function(){
                  $(".removeTime").live("click",function(){
                   alert("hello");
                  });
              });​ 
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2016-02-12
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多