天天看點

JS、html打開超連結的幾種形式

1、直接使用input在原有的标簽頁中直接打開一個頁面,将原有标簽頁覆寫

在按鈕中直接打開一個連接配接,這裡不需要用到js的代碼,根據HTML中的onclick屬性

​​<input type="button" name="btnEdit" value="編輯" onclick="window.location.href='<?=base_url()?>index.php/admin/expert/expertEdit/<?=$expertId?>';" id="btnEdit" class="input" />​​      

2、JS打開超連結的幾種形式

window.open(''url'')        打開一個新的标簽頁

​​'#gradePaper').click(function(){
        window.open('<?=base_url()?>index.php/admin/search/searchAllByCode');
    });  ​​      
  • 用自定義函數
​​<script>
         function openWin(tag,obj)
         {
             obj.target="_blank";
             obj.href = "Web/Substation/Substation.aspx?stationno="+tag;
             obj.click();
         }
        </script>

<a href="javascript:void(0)" onclick="openWin(3,this)">超連結</a>​​      
  • window.location.href="";     這種方式也是覆寫原有的标簽頁的方式打開

3、js和jquery控制超連結,使連結在子視窗打開

  • 這是用jquery,讓其所有超連結在新視窗打開
<script type="text/javascript" src="JQuery/jquery-1.4.2.js"></script>
<script type="text/javascript">
     $(document).ready(function() {
           $("a").attr("target","_blank");
})
</script>      
  • 用jquery,想讓一部分超連結在新視窗打開,隻要在基範圍加個id就好了,比如:
​​
<div id="ccc"><a href="index.html">首頁</a></div> 
  <script type="text/javascript" src="JQuery/jquery-1.4.2.js"></script>
<script type="text/javascript">
     $(document).ready(function() {
           $("div#ccc a").attr("target","_blank");
})
</script>
​​      

作者:少帥

繼續閱讀