It is known that the execution order of a tag is onclick->href attribute
当点击浏览器a标签的时候,浏览器的默认机制如下: 1、触发a的click事件 2、读取href属性的值 3、如果是URI则跳转 4、如果是javascript代码则执行该代码
How to change this mechanism so that when the onclick event is completed, the URL that executes the href attribute will jump. The function in the onclick event sends an ajax request and performs the href attribute according to the return value. Modified
After modifying the href attribute, you need to open a new page in the current browser
update------------------------2017.06.30------------------ ------------------
After testing, the ajax request was modified to be executed synchronously, but it still failed to complete the onclick function of the a tag and then execute the href action
The reason may be that ajax is modified to a synchronous request, which will block other operations on the current page.
But the click of the a tag has been completed, and the subsequent href action continues to be executed, and the href action is void(0) at this time, and the ajax request has not returned yet
Back, that is to say, the ajax synchronization request does not block the action of the a tag
Looking forward to better answers
$('a').click(function(e) {
var _ = $(this)
$.get(xx, function() {
});
})
Use js to jump to the page in onclick
//ajax start
success:function(){
}
1. Prohibit a tag from jumping href="javascript:void(0)"
2. Request ajax in the onclick method. After success, bind the return value to href
Why not assign a value to
href
first, and then jump after the request is completed?All default events in the browser can be disabled using
event.preventDefault()
. The rest is in your callback function, and you can do whatever you want. Of course, if you need to be compatible with IE8 and The following, compatible writing methods are as follows:After a long wait, please eat