你可以做的是重视甚至按钮,而不是如果锚链接和return false
HTML
<a href="http://www.somesite.com/sometrack.mp3" onclick="return false;">
<button id="button"> Download </button>
</a>
jQuery的
$(function() {
$('#button').on('click', function() {
trackDownload();
alert($(this).parents('a').attr('href'));
window.location = $(this).parents('a').attr('href');
});
});
function trackDownload() {
alert('Track is getting download');
}
,如果你想到o笔在新标签中使用window.open($('#myanchor').attr('href'));
DEMO
作为指定trackDownload()中的注释,这是AJAX功能,你可以做什么是
$('#button').on('click', function() {
trackDownload($(this).parents('a')); // pass your link to this function
});
function trackDownload(element) {
$.ajax(
url: 'yoururl',
type: 'post',
... // all other parameter you want to pass
success: function() { // success when ajax is completed
window.location = element.attr('href'); // change url only when ajax is success
}
}
但是,如果要附加单击事件链接,然后你可以做同样的事情,只需要连接事件
<a id='music' href="http://www.somesite.com/sometrack.mp3" onclick="return false;">
<button> Download </button>
</a>
个JS
$(function() {
$('#music').on('click', function() {
trackDownload();
alert($(this).attr('href'));
window.location = $(this).attr('href');
});
});
您也可以看看这个对于更多的选择How can I simulate a click to an anchor tag?
假设链接位于元素ID #container内。 –