使用JQuery和CSS類比超連結的使用者單擊事件的實現代碼

來源:互聯網
上載者:User

在正式開始本文之前,先來簡單介紹下HTML的<a>標籤:使用<a>標籤,我們可以在HTML頁面上定義錨(anchor),錨有兩種用法:
通過使用 href 屬性,建立指向另外一個文檔的連結(或超連結)
通過使用 name 或 id 屬性,建立一個文檔內部的書籤(也就是說,可以建立指向文檔片段的連結)
本文的內容與錨的第一種用法有關。 複製代碼 代碼如下:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Web.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {

// 單擊spanAGo,調用超連結的單擊事件
$('#spanAGo').click(function () {
$('#aGo').click();
});
});
</script>
</head>
<body style="font-size: 12px;">
<form id="form1" runat="server">
<div>
<a id="aGo" href="http://www.jb51.net/">指令碼之家</a>
<br />
<br />
<span id="spanAGo" style="border: 1px solid black;">點擊我,將調用以上超連結的單擊事件</span>
</div>
</form>
</body>
</html>

以上代碼的運行效果如所示:

點擊超連結,頁面可以正常跳轉;但點擊標籤,頁面卻不可以跳轉;以上,在IE8和Chrome裡都無法跳轉(其他瀏覽器未測試)。所以,接下來要實現的效果,就是在點擊標籤的時候讓頁面跳轉(也就是在調用超連結的單擊事件時,讓頁面跳轉),且寫的代碼要少,且最好是在一個地方處理,一個項目不可能就一個頁面,一個頁面不可能就一個超連結,且不能做的太死,怎麼說錨的另一個作用是書籤,別連結是可以跳轉了,錨的書籤作用被屏蔽了,且……。

Main.css 複製代碼 代碼如下:a.forward
{
}

Main.js 複製代碼 代碼如下:/// <reference path="jquery-1.4.1-vsdoc.js" />
$(document).ready(function () {
// 使超連結支援click事件,方便JavaScript調用
$('a.forward').click(function () {
location.href = $(this)[0].href;
return false;
});
});

修改過後的頁面源碼如下: 複製代碼 代碼如下:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Web.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<link type="text/css" rel="Stylesheet" href="Styles/Main.css" />
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="Scripts/Main.js"></script>
<script type="text/javascript">
$(document).ready(function () {

// 單擊spanAGo,調用超連結的單擊事件
$('#spanAGo').click(function () {
$('#aGo').click();
});
});
</script>
</head>
<body style="font-size: 12px;">
<form id="form1" runat="server">
<div>
<a id="aGo" class="forward" href="http://www.jb51.net">指令碼之家</a>
<br />
<br />
<span id="spanAGo" style="border: 1px solid black;">點擊我,將調用以上超連結的單擊事件</span>
</div>
</form>
</body>
</html>

運行一下(略),點擊標籤,頁面完美跳轉,(*^__^*) 嘻嘻好了,最後來總結一下,類比超連結的使用者單擊事件,我們需要做的就是:
匯入外部CSS檔案,Main.css,匯入外部JavaScript檔案Main.js(必須在匯入JQuery檔案之後匯入);
給超連結添加CSS類“forward”;
然後3是什麼呢?然後想不出來然後了。
最後祝大家敲代碼愉快。

首發:部落格園->劍過不留痕

相關文章

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.