0

我可以将图像放在跨度内并使用 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>
4

7 回答 7

1

这是您可能需要包含 jQuery的工作,这篇文章讲述了如何在页面上添加 jQuery

现场演示

$(document).ready(function(){
    $(".removeTime").click(function(){
     alert("hello");
    });
});​
于 2012-11-23T05:03:24.440 回答
1

将跨度的 CSS 设置为display:block;或将跨度替换为<div>

于 2012-11-23T05:05:00.427 回答
0

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

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

$(document).ready(function(){
    $(".removeTime").live("click",function(){
     alert("hello");
    });
});​ 
于 2012-11-23T06:19:01.503 回答
0

确保在应用任何文档就绪函数之前包含 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>
于 2012-11-23T05:07:54.940 回答
0

确保您已在文件的 head 部分中包含 jquery。由于代码工作正常。

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

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

于 2012-11-23T05:08:33.387 回答
0

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

检查工作的jsfiddle

js代码的小改进:

$(function() {
    $(".removeTime").on('click', function(){
         alert("hello");
    });
});​
于 2012-11-23T05:10:01.167 回答
0

此代码正在运行 请注意代码中的一些要点

1) 不要忘记将 http://code.jquery.com/jquery-1.8.3.min.js等最新的 jquery 文件添加到您的代码中。

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

于 2012-11-23T05:18:19.393 回答