事件点击href<a>标签
Event click on href <a> tag
我有一个关于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.open
或windown.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>
检查这个代码片段,它也会像你想做的那样工作。
- 在<页眉>标签
- Ckeditor-plugin:插入虚假元素add不情愿<p>标签前后
- </脚本>标签放错地方了
- Jquery在点击<a>标签
- Ionic V1.3-标签-带条件的徽章样式<离子选项卡>
- <选择>标签不起作用
- 如何在<嵌入>标签在Plone 4.1中
- 在<script src=“"></脚本>标签
- 使用几个<脚本>标签不会't工作-只调用一个脚本
- 如何在更新面板内部调用/触发javascript函数<触发器>标签
- react-让一个元素返回两个相邻的<tr>标签
- <h1>标签在离子含量元素中不可见
- VS代码:在<脚本>标签
- 在</表单>标签
- Meteor.js:<脚本>标签不't在<身体>
- Highlight.js为每个<代码>标签
- 聚合物-如何从<a>标签
- 当通过<a>标签,它无法正确加载
- HTML上的Webpack JSX错误<来源>标签
- 如何使角度显示为空<p>标签