[英]Dynamically create anchor tag
我有一個名為“ txt_contact”的文本框。 用戶填寫他的聯系人,包括他的電子郵件ID,例如:
Contact: Reggie Frederick, Email: regtnt@rediffmail.com; Mob: +91 984 807 7749
我將此信息保存在數據庫中。 但是我想將“ regtnt@rediffmail.com”作為定位標記,並且單擊它時應將其重定向到rediffmail.com。 如何做到這一點?
這是我已經嘗試過的。
string cont = txt_contact.Text;
Regex regx1 = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);
MatchCollection matches = regx1.Matches(cont);
foreach (Match match in matches)
{
cont = cont.Replace(match.Value, "<a class='abc' href='" + match.Value + "'>" + match.Value + "</a>");
}
注意:用戶可以在文本框中填寫任何電子郵件帳戶(gmail,yahoo等)
這對我有幫助:
顯示數據時,我正在為電子郵件編寫正則表達式。
$(document).ready(function () {
var regEx = /(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)/;
var contact = $("span[id*=lblContact]")
$(contact).filter(function () {
return $(this).html().match(regEx);
}).each(function () {
$(this).html($(this).html().replace(regEx, "<a style=color:blue href=\"mailto:$1\">$1</a>"));
});
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.