当用户点击链接时,HTML或
JavaScript中是否有任何选项,如果该链接可用则完成,如果没有,则进入另一个链接.
这是我的代码的一部分:
<div id="sidebar-collapse" class="col-sm-3 col-lg-2 sidebar"> <ul> <li><a href="/returns"><span class="glyphicon glyphicon-log-in"></span> Returns</a></li> <li class="active"><a href="/pay_reco" target="_blank"><span class="glyphicon glyphicon-log-in"></span> Payment Recouncillation </a></li> <li ><a href="/pay_get" target="_blank"><span class="glyphicon glyphicon-log-in"></span> Payment Get </a></li> <li><a href="/import"><span class="glyphicon glyphicon-log-in"></span> Import </a></li> <li> <!-- Here I want to add Two link in a href tag if first link does not available (web page not available) then it go to another link <!-- <object data="http:/127.0.0.1:9004" type="href" > --> <a target="_blank" href="http://127.0.0.1:9001"> <span class="glyphicon glyphicon-log-in"></span> ComPare </a> </li> <li><a target="_blank" href="http://127.0.0.1:9000"><span class="glyphicon glyphicon-log-in"></span> Portal </a></li>
解决方法
第1步:更新您的HTML代码,如下所示.
<a id="selector" href="javascript:void(0)"> <span class="glyphicon glyphicon-log-in"></span> ComPare </a>
第2步:更新HTML后,使用jQuery处理click事件并发送ajax请求,如果响应正常,请转到链接1,或者转到用户另一个URL的其他情况.
$(function(){ $('body').on('click','#selector',function() { $.ajax({ type: "GET",url: "http://127.0.0.1:9001",complete: function(xhr,textStatus) { if(xhr.status == 200) window.location = "http://127.0.0.1:9001"; else window.location = "http://127.0.0.1:9004"; } }); }) })
使用AnotherJS:
HTML:
<a href="http://127.0.0.1:9001" data-another="http://127.0.0.1:9004">Link Text</a>
使用Javascript:
$(function() { $('a').another(); })
就这样! 原文链接:https://www.f2er.com/html/225879.html