Javascript 当用户使用Jquery单击锚定标记时,如何重定向到另一个页面?
当用户使用jquery而不是href=“”单击链接时,我希望重定向到另一个页面 我尝试了下面的代码。但它不起作用Javascript 当用户使用Jquery单击锚定标记时,如何重定向到另一个页面?,javascript,jquery,html,Javascript,Jquery,Html,当用户使用jquery而不是href=“”单击链接时,我希望重定向到另一个页面 我尝试了下面的代码。但它不起作用 $('.list-of-link').on('click', 'a', function () { alert($(this).html()); window.location.replace("http://stackoverflow.com"); }) 警报正在工作。但重定向或替换不起作用 <div class="list-of-link">
$('.list-of-link').on('click', 'a', function () {
alert($(this).html());
window.location.replace("http://stackoverflow.com");
})
警报正在工作。但重定向或替换不起作用
<div class="list-of-link">
<a class="SavedLink" href="">jjj</a><br>
<a class="SavedLink" href="">jassom</a><br>
<a class="SavedLink" href="">filetest</a><br>
<a class="SavedLink" href="">testsucces</a><br>
<a class="SavedLink" href="">faisal</a><br>
<a class="SavedLink" href="">tajul</a><br>
<a class="SavedLink" href="">description</a><br>
<a class="SavedLink" href="">description1</a><br>
</div>
更新
这些链接是由ajax完成的。通过以下代码:
var UserID=$('hiddensessionserid').val()
$.ajax({
url:'SavedReports.aspx/getSavedReportName',
方法:“post”,
contentType:'应用程序/json',
数据:“{userID:”+userID+“}”,
数据类型:“json”,
成功:功能(数据){
如果(data.d!=“无报告”){
//警报(“成功”+data.d);
var fullData=data.d;
var splitData=fullData.split(“$$”);
对于(变量i=0;i ”);
}
}
},
错误:函数(错误){
警报(“请致电管理员”);
}
})
你需要这样做
这将停止锚定标记执行它应该执行的操作,并使用href属性进行导航。
如果不想在JS中执行此操作,也可以从HTML中删除href属性
编辑:刚刚看到您的额外代码。
你可以改变
$(".list-of-link").append('<a class="SavedLink" href="" >' + splitoneLinkData[1] + '</a><br/>');
$(“.list of link”).append(“
”);
到
$(“.list of link”).append(“+splitoneLinkData[1]+”
);
试试这个:$('.list-of-link').on('click', 'a', function (e) {
e.preventDefault();// add this line
alert($(this).html());
window.location.replace("http://stackoverflow.com");
})
您可以使用:
$('.list-of-link').on('click', 'a', function () {
window.location.href = "http://stackoverflow.com";})
您可以将href=“”
更新为href=“;”
或href=“javascript:;”
在锚href中指定#,其余代码按预期执行:
<a class="SavedLink" href="#">jjj</a>
代码应该可以工作,上面的代码没有错误,您收到警报了吗?不要使用全局“a”标记,定义一个类。最好使用窗口。位置。替换(“http://stackoverflow.com");
console中是否出现错误?为什么要删除一些在每个浏览器中都能正常工作的功能,无论是否使用javascript?如果我添加e.preventDefault()代码>即使警报框也不工作。我在移动到函数的最后一行后尝试well@mohamedfaiz是否在函数参数中添加了e
?
$('.list-of-link').on('click', 'a', function (e) {
e.preventDefault();// add this line
alert($(this).html());
window.location.replace("http://stackoverflow.com");
})
$('.list-of-link').on('click', 'a', function () {
window.location.href = "http://stackoverflow.com";})
<a class="SavedLink" href="#">jjj</a>