【问题标题】:Launch Jquery function when link clicked单击链接时启动 Jquery 功能
【发布时间】:2013-06-01 14:04:14
【问题描述】:

我已经尝试了一段时间,但我找不到解决方案,希望您能提供帮助。

我有一个这样的 youtube 视频的链接

    <a id='LinkClick1' href='www.youtube.com' target='_blank'><strong>Title</strong></a>

并且我希望在单击该链接时进行注册,但是每次单击它都会在另一个选项卡中打开该网站,并且不会执行我在我的功能中所做的事情

    $("#LinkClick1").click( function(){
      var a='Hi!';
      var clicked='yes';
      console.log(a+clicked);
    });

如何让我的链接点击打开网站,同时触发功能?

谢谢。

【问题讨论】:

  • 您的代码正是这样做的:jsfiddle.net/tymeJV/CE2k5
  • 除了console.log,我没有在您的点击函数中看到函数调用。你是说点击链接时console.log 没有触发?
  • 我怀疑您分配“click”处理程序的代码在 &lt;a&gt; 元素添加到 DOM 之前正在运行。将您的&lt;script&gt; 移动到&lt;body&gt; 的最后。
  • $(document).ready(function(){..}中添加你的代码
  • 您的链接是否在运行时添加(通过 ajax)?

标签: jquery function hyperlink


【解决方案1】:

另一个 DEMO... 与问题中的评论中提到的弹出框内的链接。

http://jsfiddle.net/weuWk/1126/

相关代码..

var img = '<a href="https://si0.twimg.com/a/1339639284/images/three_circles/twitter-bird-white-on-blue.png" target="_blank" id="1">TEST</a>';
$("#blob").popover({
  trigger: "manual",
content: img    
}).on("click", function(e) {
  e.preventDefault();
}).on("mouseenter", function() {
  $(this).popover("show");
  $(this).siblings(".popover").on("mouseleave", function() {
    $(this).hide();
  });
}).on("mouseleave", function() {
  var _this = this;
  setTimeout(function() {
    if (!$(".popover:hover").length) {
      $(_this).popover("hide")
    }
  }, 100);
});

$(document).on('click', '#1',function(){
console.log("CLicked");
});

考虑到 bootrstrap 弹出框进行更新。如果它仍然不适合您,请更好地显示 HTML,甚至更好地在 jsfiddle 上显示您的问题 DEMO。

http://jsfiddle.net/AFffL/547/

<a class="popup-marker btn" data-content="Click outside the popover to close it." data-original-title="Popover A" href="youtube.com" target="_blank">Click me (A)</a>

jQuery(function() {


    $('.popup-marker').popover().on('click', function(e) {
        // if any other popovers are visible, hide them
        var a='Hi!';
       var clicked='yes';
      console.log(a+clicked);
    });


});

_------------------------------------------------ ------

您显示的代码确实按照您说的做... 在

上查看演示

http://jsfiddle.net/tymeJV/CE2k5/(.. 由 tymeJV 在评论中)...

但是,如果链接在运行时由dom manipulationAjax 加载,那么您的代码可能无法正常工作。 在这种情况下,请尝试以下代码。

$(document).on('click','#LinkClick1',function(){
      var a='Hi!';
      var clicked='yes';
      console.log(a+clicked);
    });

你可以使用#LinkClick1Selector for parent代替$(document)

【讨论】:

  • +1 用于事件委托,而不是千篇一律的“将其放入 .ready()”
猜你喜欢
  • 2012-01-18
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-28
  • 2015-03-18
  • 1970-01-01
相关资源
最近更新 更多