事件点击href<a>标签

Event click on href <a> tag

本文关键字:gt 标签 lt href 事件      更新时间:2023-09-26

我有一个关于Javascript事件的问题。我有一个像这样的<a>标签:

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

然后我称一个事件为:

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

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

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

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

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

在我的业务中,我只需要一个<a>标签,所以不需要window.openwindown.location

说明

只有当用户直接点击链接上的时,才能进行重定向。编程或延迟的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

Js投标示例

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);

试试这个-

<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");

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

jQuery fiddle-http://jsfiddle.net/gcJ73/3/

尝试:

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

jQuery('#aTag'(.click((不执行锚点标记的href属性,因此不会重定向,请执行:

$(document).ready(function() {
    jQuery('#aTag').click( function (e) {
        window.location.href = this.href;
    });
});
<a id='aTag' href='http://mylink.com' onclick="location.href='http://mylink.com';">
Click to redirect
</a>

检查这个代码片段,它也会像你想做的那样工作。