如何給按鈕加上連結功能

來源:互聯網
上載者:User

指令碼說明:
把如下代碼加入<body>地區中
<INPUT TYPE="submit" value="建站資源網" onclick="location.href='http://www.jzzy.com'">

按鈕連結乾坤大挪移  
作為一個小技巧,暫時歸類到ASP中。
在設計網站的時,我們可能會想把連結做成按鈕的樣子,按鈕做成連結的樣子。下面說一下我的方法。
1、按鈕做成連結(圖片)的樣子
提交按鈕<input type="submit" value="提交">
提交連結<a href="#" onclick="表單名字.submit()">提交</a>
重設按鈕<input type="reset" value="重設">
重設連結<a href="#" onclick="表單名字.reset()">重設</a>
普通按鈕<input type="button" value="按鈕" onclick="函數()">
普通連結<a href="#" onclick="函數()">連結</a>
至於圖片也一樣把a標籤換成img
2、連結做成按鈕的樣子
<a href="reg.asp">註冊</a>
=><input type="button" value="註冊" onclick="location.href='reg.asp'">
-----------------------------------
有的時候我們完全可以手工做一個get方式的表單,至於用按鈕還是連結隨心所欲。
<form action="xx.asp" method="get" name="form1">
  <input name="aa" type="text" id="aa">
  <input name="bb" type="text" id="bb">
  <input type="submit" name="Submit" value="提交">
</form>
=>
<input name="aa" type="text" id="aa">
<input name="bb" type="text" id="bb">
<input type="button" value="按鈕" onclick="location.href='xx.asp?aa='+document.all['aa'].value+'&bb='+document.all['bb'].value">
-----------------------------------
進一步說我們還可以做一個按鈕(連結)來同時傳遞js變數,表單input的值,asp變數,Recordset值
<script language="javascript">
var id1=1;
</script>
<%
id3=3
....
rs.open exec,conn,1,1
假設有rs("id4")=4
...
%>
<input name="id2" type="text" id="id2" value="2">
<input type="button" value="按鈕"
onclick="location.href='xx.asp?id1='+id1+'&id2='+document.all['id2'].value+'&id3=<%=id3%>&id4=<%=rs("id4")%>'">
我們按下按鈕會看到瀏覽器的url是xx.asp?id1=1&id2=2&id3=3&id4=4
在xx.asp中我們就可以用request.querystring來得到所有變數,這樣是不是變相的用戶端js和伺服器段的變數傳遞?
------------------------------------------------------------------------------------------------------------------------------
如何給按鈕加上連結功能

解決思路:
按鈕屬於控制項級的對象,優先順序比較高,所以不能象圖片或文本一樣直接加連結,只能通過按鈕的單擊事件呼叫指令碼的方式來實現。
具體步驟:
    1.在原視窗開啟連結
    <input type="button"   value="閃吧" onClick="location=’http://www.flash8.net’">
    <button onClick="location.href=’http://www.flash8.net’">閃吧</button>
    <form action="http://www.flash8.net"><input type="submit" value="開啟連結"></form>
    2.在新視窗中開啟連結
    <input type="button"  
value="閃吧" onClick="window.open(’http://www.flash8.net’)">
    <button onClick="window.open(’http://www.flash8.net’)">閃吧</button>
    <form action="http://www.flash8.net"  
target="_blank"><input type="submit" value="開啟連結"></form>
注意:onClick調用的代碼裡的引號在只有一重時可以單雙嵌套,超過兩重就必須用"/"號轉義且轉義的引號必須跟裡層的引號一致,如:
<button onClick="this.innerHTML=’<font color=/’red/’>http://www.flash8.net</font>’">閃吧</button>

<button onClick=’this.innerHTML="<font color=/"red/">http://www.flash8.net</font>"’>閃吧</button>
而下面都是錯誤的寫法:
<button onClick="this.innerHTML=’<font color=’red’>http://www.flash8.net</font>’">閃吧</button>
<button onClick="this.innerHTML=’<font color="red">http://www.flash8.net</font>’">閃吧</button>
<button onClick="this.innerHTML=’<font color=/"red/">http://www.flash8.net</font>’">閃吧</button>
提示:大部分屬於window或document對象的方法和屬性都可以省略首碼window或document,比如說本例中的location.href(location.href又可以簡寫為location,因為location的預設對象為href)就是window.location.href或document.location.href的省略式寫法。
技巧:本例中還可以用下面的方法來代替location.href
location.replace(url)
location.assign(url)
navigate(url)
特別提示
第一步中的代碼運行後,單擊按鈕將跳轉到連結目標。而第二步的在單擊按鈕後將在新視窗中開啟連結。
特別說明

本例主要是通過用onClick捕獲使用者在按鈕上的單擊事件,然後調用location對象的href方法或window對象的open方法來開啟連結。另外一個技巧是通過提交表單來實現連結功能,按鈕必須是type=submit類型的按鈕,表單的action值就是連結目標,target值就是連結開啟的目標方式。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.