【问题标题】:Event click on href <a> tag [duplicate]事件点击href <a>标签[重复]
【发布时间】:2014-08-24 06:40:21
【问题描述】:

我有一个关于 Javascript 事件的问题。我有一个像这样的&lt;a&gt; 标签:

&lt;a id='aTag' href='http://example.com'&gt;Click to redirect&lt;/a&gt;

然后我调用如下事件:

<script>
$('#aTag').click();
//or
$('#aTag').trigger('click');
</script>

它不会将我重定向到http://example.com。我尝试在&lt;a&gt; 标签中添加一个onClick() 事件,如下所示:

<a id='aTag' href='http://example.com' onclick='alert("Event happened");'>Click to redirect</a>

然后调用.click() 事件。它告诉我alert("Event happened")

谁能告诉我如何正确调用.click() 事件,或者更正此重定向与href&lt;a&gt; 标记中的问题?

在我的业务中,我只需要一个&lt;a&gt; 标签,所以不需要window.openwindown.location

【问题讨论】:

  • 为什么首先需要一个事件来重定向?
  • 就像@Mr.Alien 说的那样,去掉jquery 脚本,它会照原样工作:jsfiddle.net/AdPtj

标签: javascript jquery ajax magento jquery-events


【解决方案1】:

说明

只有当用户直接点击链接时,才会发生重定向。程序化或延迟click 触发器不起作用。

另一种选择是:

  • 直接更改window.location
  • 在新标签页/窗口中打开链接

更改window.location

window.location="http://wherever.you/want/to/g0";

 window.location=$('a.selector').attr('href'); // to use a link's address

新标签/窗口

您不能以编程方式(click()trigger)打开新选项卡/窗口或重定向。他们被(广告)屏蔽。自动

因此,新标签/窗口的打开始终必须由用户操作触发。 (否则我们总是会被弹出广告填满)

首先,确保你的 js 在用户事件上执行,然后你应该能够使用window.open

JsFiddle example

html:

<a href="//google.com" target="blank">new tab google</a>

<button class="user">user triggered</button>
<button class="programatic">programatic</button>

js:

$('a').on('click', function(e) {
    console.log('clicked', e);
    // unfortunately although we simulated 
    // the click on the <a/> , it will still 
    // not launch a new window - idk why.
    // therefore we can use the line below
    // to open the <a>'s href in a new tab/window
    // NOTE: this will only occur if the execution was
    // triggered by the user
    window.open(e.currentTarget.href);
});

var simulateClick = function(origEv) {
    var e = $.Event("click");
    e.ctrlKey = true;
    e.metaKey = true;
    e.originalEvent = origEv;
    $('a').trigger(e);
};

$('button.user').on('click', function(e) {
    // this call will actually open a window
    simulateClick(e);
});

$('button.programatic').on('click', function(e) {
    // this will result in a blocked popup
    $.get('/someurl').always(function() {
        // executes the method after a non-user event
        // results in blocked popup
        simulateClick(e);
    });
});

// this will result in a blocked popup
setTimeout(simulateClick, 1000);

【讨论】:

  • 我不认为他正在尝试打开新标签,OP 正在尝试重定向。用他的话来说——“它不会将我重定向到'mylink.com'。”
【解决方案2】:

试试这个 -

<a id="aTag" href="http://mylink.com" onclick="return doWork();">Click to redirect</a>
<script>
    function doWork(){
          //... do your works
          return true; // then return true to redirect
    }
</script>

这是小提琴 - http://jsfiddle.net/gcJ73/

(虽然 fiddle 的属性有点不同,以表明它有效)

或使用 jQuery:

//first assign the click handler
$('#aTag').click(function(){
    //... do your work
    return true;
});

//then call programmatically
$("#aTag").click(); or  $("#aTag").trigger("click");

顺便说一句,以编程方式调用它不会重定向。只会调用事件处理程序,而不是重定向。

jQuery 小提琴 - http://jsfiddle.net/gcJ73/3/

【讨论】:

    【解决方案3】:

    试试:

    <script>
        jQuery('#aTag').click(function() {
            // Your Code
        }); 
    </script>
    

    【讨论】:

      【解决方案4】:

      jQuery('#aTag').click() 不执行锚标记的href 属性,因此您不会被重定向,这样做:

      $(document).ready(function() {
          jQuery('#aTag').click( function (e) {
              window.location.href = this.href;
          });
      });
      

      【讨论】:

        【解决方案5】:
        <a id='aTag' href='http://mylink.com' onclick="location.href='http://mylink.com';">
        Click to redirect
        </a>
        

        检查此代码 sn-p,它也会像您想要的那样工作。

        【讨论】:

          猜你喜欢
          • 2019-05-15
          • 2011-05-16
          • 1970-01-01
          • 2018-01-05
          • 2016-04-27
          • 2011-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多