Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
網頁設計中,a標簽是用來創建超鏈接的標簽。通過a標簽,我們可以方便地實現頁面之間的跳轉。當用戶點擊a標簽時,瀏覽器會根據標簽中的href屬性的值來加載相應的頁面。a標簽的使用非常廣泛,可以用于跳轉到其他頁面、下載文件、發送郵件等。
在使用a標簽時,我們需要注意以下幾點:
1. href屬性:href屬性用于指定鏈接的目標地址。可以是一個絕對路徑(如http://www.example.com)或相對路徑(如./about.html)。如果要在當前頁面打開鏈接,可以使用空值或者#作為href屬性的值。
2. target屬性:target屬性用于指定鏈接在何處打開。常用的取值有_blank(在新窗口打開)、_self(在當前窗口打開)、_parent(在父窗口打開)、_top(在頂層窗口打開)。如果不指定target屬性,則默認在當前窗口打開。
3. title屬性:title屬性用于為鏈接添加提示信息。當用戶將鼠標懸停在鏈接上時,會顯示title屬性中的內容。
4. rel屬性:rel屬性用于指定鏈接與當前頁面的關系。常用的取值有nofollow(告訴搜索引擎不要跟蹤鏈接)、noopener(防止通過鏈接打開的新窗口訪問原始頁面)、noreferrer(防止通過鏈接打開的新窗口訪問原始頁面并且不傳遞referrer信息)。
5. download屬性:download屬性用于指定鏈接下載文件時的文件名。當用戶點擊鏈接時,瀏覽器會自動下載指定的文件,并使用download屬性指定的文件名保存。
除了以上幾點,還可以通過CSS樣式來美化a標簽,如改變鏈接的顏色、添加下劃線等。
a標簽是實現頁面跳轉的重要標簽,合理的使用a標簽可以提升用戶體驗,增加網站的功能。
們今天來分析解釋一下這個表達式string hrefPattern = @"href\s*=\s*(?:""'[""']|(?<1>[^>\s]+))";,并用實例演示用法。這個正則表達式用于從文本中提取href屬性的值,這些值可以是被單引號或雙引號包圍的,或者是不包含大于符號和空白字符的文本。我們分解這個正則表達式來詳細解釋它的各個部分:
1. href\s*=\s*: 這部分匹配 href 關鍵字,后面可以跟著零個或多個空白字符,然后是一個等號,再然后又是零個或多個空白字符。其中href: 直接匹配文本中的"href",這是HTML中表示鏈接地址的屬性名稱。\s*=\s*: 匹配等號(=),等號前后可以有0個或多個空白字符(包括空格、制表符、換行符等)。
2. (?:...): 這是一個非捕獲組,意味著它會匹配括號內的內容,但不會為其創建一個捕獲組。這意味著我們不能直接從匹配結果中提取這部分內容。
3. [""'](?<1>[^""']*)[""']: 這部分匹配被單引號或雙引號包圍的任何內容。具體來說:
1. [""']: 匹配一個單引號或雙引號。
2. (?<1>[^\"']*): 創建了一個命名捕獲組,名為1,用來捕獲在引號之間的任何非引號字符序列,這就是href屬性的值。(?<1>...): 這是一個命名捕獲組,但這里它被放在了一個非捕獲組內,這意味著它不會捕獲匹配的內容。
3. [^""']*: 匹配任何不是單引號或雙引號的字符零次或多次。
4. [""']: 再次匹配一個單引號或雙引號。
4. |: 或者操作符,表示前面的模式和后面的模式中的任何一個可以匹配。又叫管道符號,代表邏輯“或”操作,也就是表示前面的模式與后面的模式任一滿足即可。
5. (?<1>[^>\s]+): 這部分匹配任何不是大于符號或空白字符的字符一次或多次。這也是一個命名捕獲組,但同樣,它被放在了一個非捕獲組內。當href值沒有被引號包圍時使用。也就是這部分匹配不是大于符號(>)和空白字符的任何字符1次或多次,但不包括引號。
綜上所述,此正則表達式能夠處理以下兩種格式的href屬性及其值:
1. 被引號包圍的情況:<a href="http://example.com">...</a> 或 <a href='http://example.com'>...</a>
2. 未被引號包圍的情況:<a href=http://example.com>...</a>
實例演示用法:
using System.Text.RegularExpressions;
namespace ConsoleAppC
{
internal class Program
{
static void Main(string[] args)
{
string inputString = @"<a href=""http://example.com"">Link</a>
<a href='http://another.example.com'>Another Link</a>
<a href=http://noquotes.example.com>No Quotes Link</a>";
string hrefPattern = @"href\s*=\s*(?:[""'](?<1>[^""']*)[""']|(?<1>[^>\s]+))";
MatchCollection matches = Regex.Matches(inputString, hrefPattern);
foreach (Match match in matches)
{
Console.WriteLine(match.Value); // 輸出匹配到的href屬性值
Console.WriteLine($"Found href value: {match.Groups[1].Value} at index: {match.Groups[1].Index}");
}
}
}
}
運行這段代碼后,將輸出如下結果:
href="http://example.com"
Found href value: http://example.com at index: 9
href='http://another.example.com'
Found href value: http://another.example.com at index: 72
href=http://noquotes.example.com
Found href value: http://noquotes.example.com at index: 150
為了給大家演示如何使用這個正則表達式,我們再看以下例子:
假設我們有以下的HTML片段:
<a href="https://www.example.com">Click here</a>
<a href='https://www.example.org'>Go there</a>
<a href="https://www.example.net" target="_blank">Open external link</a>
使用上述的正則表達式,我們可以提取所有的href屬性值:
string input = @"<a href=\""https://www.example.com\"">Click here</a>
<a href='https://www.example.org'>Go there</a>
<a href=\""https://www.example.net\"" target=\""_blank\"">Open external link</a>";
代碼為:
string hrefPattern = @"href\s*=\s*(?:[""'](?<1>[^""']*)[""']|(?<1>[^>\s]+))";
Regex regex = new Regex(hrefPattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection matches = regex.Matches(input);
foreach (Match match in matches)
{
Console.WriteLine($"Found href: {match.Groups["1"].Value}");
}
string input = @"<a href=\""https://www.example.com\"">Click here</a>
輸出將是:
Found href: \"https://www.example.com\"
Found href: https://www.example.org
Found href: \"https://www.example.net\"
注意,這個正則表達式并不完美,它可能無法處理所有可能的HTML格式,但對于簡單的用途來說可能已經足夠了。
TML常用標簽有:a標簽、table標簽、img標簽、form標簽和input標簽。
作用
屬性
(一)href
href是hyper reference的縮寫,超鏈接的意思。
用于指定鏈接目標的ur地址,(必須屬性)當為標簽應用href屬性時,它就具有了超鏈接的功能;
href=“#”表示這是一個空鏈接;
如果href里面地址是—個文件或者壓縮包,會下載這個文件。
<a href="https://google.com">超鏈接到google網站的主頁</a>
<a href="https://google.com">超鏈接到google的主頁</a>
<a href="//google.com">超鏈接到google的主頁</a>
展現形式:
點擊此鏈接,即可到達google的主頁
a標簽href的取值:
1、上述代碼中的網址的取值(推薦使用第三行的代碼)
<a href="//google.com">超鏈接到google的主頁</a>
由于此方式能夠自動補齊相關的網絡地址,前面兩種寫錯就會報錯,所以推薦使用。
2、路徑
當前路徑下的a里面的b,b里面的c
在當前目錄下尋找index.html文件
3、偽協議
<a href="javascript:;">點擊后無任何點擊或刷新等動作的反應</a>
<a href="#要跳轉的元素的id"></a>
點擊鏈接的時候,會跳轉到指定元素所在的位置。
<a href="mailto:abcdefg@163.com ">發郵件給我</a>
<a href="tel:12345678901">打電話給我</a>
(二)targe
用于指定鏈接頁面的打開方式
a的target取值
1、內置名字
_blank 在空白頁打開
_self 在當前頁面打開
_parent 在父級窗口打開
_top 在最頂級的窗口打開
<a href="//google.com" target="_blank">超鏈接到google網站的主頁在空白頁打開</a>
2、程序員的命名
window:name(在xxx頁面打開)
iframe的name(iframe現在已經很少使用了,是指內嵌窗口)
(三)download
下載頁面,但目前很少用,有的瀏覽器不支持,尤其是手機瀏覽器可能不支持。
1、table標簽的語法:
thead:表頭
tbody:表的內容,用于定義
tfoot:表的腳部
tr:table row,表格里的行
th:表格的表頭部分,其中的文本內容字體加粗居中顯示
td:table data,表格數據,用于定義表格中的單元格
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<table>
<thead>
<tr>
<th></th>
<th>小紅</th>
<th>小黃</th>
<th>小藍</th>
</tr>
</thead>
<tbody>
<tr>
<th>數學</th>
<td>90</td>
<td>60</td>
<td>80</td>
</tr>
<tr>
<th>語文</th>
<td>88</td>
<td>95</td>
<td>97</td>
</tr>
<tr>
<th>英語</th>
<td>88</td>
<td>95</td>
<td>97</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>總分</th>
<td>266</td>
<td>250</td>
<td>274</td>
</tr>
</tfoot>
</table>
</body>
</html>
第一行的th標簽為空
2、table的樣式
table-layout:auto;自動計算每一行的寬高
table-layout:fixed;通過列表的寬度來決定平均寬度
border-collapse:collapse; 合并邊框(默認邊框與邊框之間不合并)
border-spacing:0;邊框為0.(邊框與邊框之間的距離)。
作用:發出get請求,展示一張圖片。
<img src="1.JPG" alt="頭像" width="400" />
當前路徑下的1.jpg,確定寬度為400,只寫寬度高度會自適應
屬性
alt:alternate的縮寫,替換的意思。替換文本,圖像不能顯示的文字。
路徑錯誤顯示alt內容
title:提示文本。鼠標放到圖像上,顯示的文字。
響應
max-width:100% 所有的圖片在手機上都自適應寬度,寬度最大為100%。
事件
onload/onerror 監聽圖片是否加載成功,加載成功時用onload,不成功是用onerror事件。確保在onerror事件能夠補救。
<body>
<img id="xxx" src="dog.jpg" alt="一只小狗">
<script>
xxx.onload = function () {
console.log("圖片加載成功");
};
xxx.onerror = function () {
console.log("圖片加載失敗");
xxx.src = "/404.jpg";
};
</script>
</body>
監聽成功時,打印出成功
監聽失敗時,先打印出監聽失敗并且開始執行加載失敗是的挽救圖片。404.jpg文件執行
本文為作者本人的原創文章,著作權歸作者本人和饑人谷所有,轉載務必注明來源。
*請認真填寫需求信息,我們會在24小時內與您取得聯系。