There is an onclick event for a button on the page. Clicking triggers js and sends an ajax request to the background. After a series of judgments, the background returns the url to js
<a class="a" id="k_id" href="javascript:void(0);" onclick="test($(this),id1,id2);"></a>
function test(){
//发送ajax请求...返回一个url
var url = "ajax请求返回的url";
//js操作将返回的url赋值到页面上的a标签,然后js模拟a标签点击事件
$(oElement).attr("href",url);
$(oElement).removeAttr("onclick");
var id = $(oElement).attr("id");
document.getElementById(id).click();//这里点击a标签
//这样的操作会被浏览器拦截,
}
求解
You can jump directly to the url. Why do you want to simulate a click? Direct window.location.href = url
onclick monitors the click event, so it triggers the call to the test method first, and then calls it repeatedly, but the herf jump cannot be triggered
After sending ajax, there is no need to simulate a tag click event, just set location.href = the URL that needs to be jumped
When jumping to the page, there is no need to remove the
onclick
event ofa
;If you need to remove it, the current method is directly
t.onclick = null;
and you canYou can try it
Open a new page: window.location.href = url;
Open a new window: window.open(url);
Isn’t it better to use window.location.href directly?
The
a tag has a default click event. It is estimated that onClick conflicts with the default event.
Because there are asynchronous operations, the browser will intercept
window.open()
.You can change ajax to synchronization, or find a way to calculate the url on the front end.