活动点击href <a> tag</a>

时间:2014-07-03 08:26:44

标签: javascript jquery ajax magento javascript-events

我在这里有关于javascript事件的问题。我有一个<a>标签,如下所示:

<a id='aTag' href='http://mylink.com'>Click to redirect</a>

然后我将事件称为:

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

它不会将我重定向到&#39; http://mylink.com&#39;。我尝试在onClick()标记中添加<a>事件,如下所示:

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

然后调用.click()事件。它显示了alert("Event happened");

有人可以告诉我如何正确调用.click()事件,或更正此href标记中<a>问题的重定向吗?

在我的商家中,我只需要一个<a>代码,而不是window.openwindown.location

5 个答案:

答案 0 :(得分:3)

解释

重定向只有在用户点击链接上的 时才会发生。程序化或延迟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)打开新标签/窗口或重定向。他们得到(ad-)阻止。自动

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

首先,确保您的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);

答案 1 :(得分:1)

试试这个 -

<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/

(虽然小提琴属性有点不同,以告诉你它有效)

或使用jQuery:

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

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

BTW以编程方式调用它不会重定向。只调用事件处理程序,而不是重定向。

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

答案 2 :(得分:0)

尝试:

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

答案 3 :(得分:0)

jQuery(&#39;#aTag&#39;)。click()不执行锚标记的href属性,因此您不会被重定向,执行:

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

答案 4 :(得分:0)

<a id='aTag' href='http://mylink.com' onclick="location.href='http://mylink.com';">
Click to redirect
</a>

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

相关问题