【问题标题】:jQuery href left click not workingjQuery href 左键单击不起作用
【发布时间】:2026-02-15 05:10:02
【问题描述】:

得到了我从一本书(新手到忍者)稍微改编的这段代码。

用户点击“联系人”,下方会打开一个表单,其中包含电话号码和 href 链接。左键单击链接没有任何作用,但我可以右键单击并“在新选项卡中打开”,然后就可以了。

对 js 和 jQuery 来说相当新,但我认为问题(我可能错了)是无法正常工作的 href 链接与打开表单的“联系”链接链接到相同的“a”,但我不知道怎么改。

      <div id="contact">
    <a href="#">Contact</a>
    <form action="">
    Phone me on:018<br><br>Email me at <a href="http://www.guardian.co.uk/lifeandstyle/2012/apr/04/samantha-brick-hated-good-looks?newsfeed=true">click me</a>
    </form>
    </div>

jQuery

  $('#contact form').hide();
  $('#contact a').toggle(function() {
  $(this)
  .addClass('active')
  .next('form')
  .animate({'height':'show'}, {
    duration:'slow',
    easing: 'swing'
  });
  }, function() {
  $(this)
  .removeClass('active')
  .next('form')
  .slideUp();
  });

非常感谢

【问题讨论】:

    标签: jquery html


    【解决方案1】:

    代替

    $('#contact a')
    

    尝试使用

    $('#contact > a')
    

    通过这种方式,您只定位直接子代而不是其他&lt;a&gt;。我认为,当您单击带有 href &lt;a href="http://www.guardian.co.uk/lifeandstyle/2012/apr/04/samantha-brick-hated-good-looks?newsfeed=true"&gt;click me&lt;/a&gt; 的链接时,您的 sn-ps 也会运行,并且脚本会中断并且因此没有遵循该链接

    【讨论】:

    • 像魅力一样工作。谢谢。