什么可能使 jQuery 的切换在第二次单击时触发两次?

发布于 2024-09-14 14:34:57 字数 1567 浏览 1 评论 0原文

我有一个 FireWorks 生成的 HTML 页面,我向其中添加了一些 jQuery 以获得所需的效果...这是为了进行疼痛研究,因此当您单击脊柱的一部分时,它会亮起红色。我添加了 jQuery 的切换以实现某种“取消单击”选项,但它没有按预期工作。 (类似于我之前问的问题,但在我看来足够不同)。

当我第一次单击时,它确实按预期亮起红色。当我第二次单击时,它会返回到未单击状态,然后立即返回到红色,就好像两个切换事件都被触发一样,速度如此之快,以至于就像第二次单击甚至没有注册一样(我知道它确实会触发;我设置了带有警报的伪调试方案,以确保第二个函数已运行)。关于我做错了什么有什么想法吗?

以下是一些相关代码:

jQuery 脚本:

<script type="text/javascript">
$(document).ready(function(){
   $("img.toggle1").click(function(){
      $("img.toggle1").toggle(function(){ 
        alert('first click');
          $(this).attr('src', 'images/cervical_s7.jpg');
      }, function(){ 
          alert('second click');
          $(this).attr('src', 'images/cervical_s1.jpg');
      });
   });
});
</script>

生成的 HTML 的值得注意的片段(包含切片创建的图像的表以及切片创建的图像映射。我删除了 onclick 行为来代替切换,而 onMouseOver 用于对不同图像的效果):

   <td rowspan="2"><img name="cervical_s1" class="toggle1" src="images/cervical_s1.jpg"
    width="185" height="121" border="0" id="cervical_s1" usemap="#m_cervical_s1"
    alt="" /></td>

<map name="m_cervical_s1" id="m_cervical_s1">
<area shape="poly" class="toggleCerv" coords="159,0,154,29,151,57,154,95,165,
       113,185,121,0,121,14,111,23,93,24,43,19,0,48,7,96,7,140,3,159,0,159,0" 
       href="javascript:;  alt="" onmouseover="MM_swapImage
       ('sign_s1','','images/sign_s2.jpg',1);"  /> </map>

tl;dr - jQuery 切换似乎在第一次单击后触发两次,因此它卡在切换位置 #2。

编辑:当我再次单击时,我会收到 3 个交替警报,然后是 4 个交替警报,等等。感谢您的有用评论。希望这能让你更容易弄清楚吗?

谢谢!

I have a FireWorks-generated HTML page and I added some jQuery to it for desired effects...it's for a pain study, so when you click a part of the spine, it lights up in red. I added jQuery's toggle for sort of an "unclick" option, but it's not working as expected. (Similar to a previous question I asked, but different enough IMO).

When I click the first time, it does light up red as expected. When I click the second time, it goes back to unclicked, then instantly goes back to red, as though both toggle events were fired, so fast that it's like the second click didn't even register (I know it does fire though; I rigged up a pseudo-debug scheme with alerts to make sure the second function was run). Any ideas on what I did wrong?

Here is some relevant code:

jQuery script:

<script type="text/javascript">
$(document).ready(function(){
   $("img.toggle1").click(function(){
      $("img.toggle1").toggle(function(){ 
        alert('first click');
          $(this).attr('src', 'images/cervical_s7.jpg');
      }, function(){ 
          alert('second click');
          $(this).attr('src', 'images/cervical_s1.jpg');
      });
   });
});
</script>

Noteworthy snippets of generated HTML (the table containing the image created by the slice, and the image map created by the slice. I removed the onclick behavior in place of the toggle, and the onMouseOver is for an effect on a different image):

   <td rowspan="2"><img name="cervical_s1" class="toggle1" src="images/cervical_s1.jpg"
    width="185" height="121" border="0" id="cervical_s1" usemap="#m_cervical_s1"
    alt="" /></td>

<map name="m_cervical_s1" id="m_cervical_s1">
<area shape="poly" class="toggleCerv" coords="159,0,154,29,151,57,154,95,165,
       113,185,121,0,121,14,111,23,93,24,43,19,0,48,7,96,7,140,3,159,0,159,0" 
       href="javascript:;  alt="" onmouseover="MM_swapImage
       ('sign_s1','','images/sign_s2.jpg',1);"  /> </map>

tl;dr - jQuery toggle seems to fire twice after the first click so it's stuck in toggle position #2.

EDIT: When I click again, I get 3 alternating alerts, then 4 alternating alerts, etc. Thanks for the helpful comment. Hopefully this makes it easier to figure out?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可爱咩 2024-09-21 14:34:57

http://api.jquery.com/toggle/

不需要注册点击()

所以代替:

$("img.toggle1").click(function(){
      $("img.toggle1").toggle(...);
   });

$("img.toggle1").toggle(...);

Look at http://api.jquery.com/toggle/

You don't need to register click().

So instead of:

$("img.toggle1").click(function(){
      $("img.toggle1").toggle(...);
   });

Put

$("img.toggle1").toggle(...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文