我希望每个外部的' A'链接以为目标设置attibute,但忽略所有内部链接
示例:
<a href="http://www.somelink here" title="Some Title">Some Text</a>
&#13;
成为:
<a href="http://www.somelink here" title="Some Title" target="_blank">Some Text</a>
&#13;
Public Class MyLinks
Inherits HyperLink
Protected Overrides Sub Render(writer As HtmlTextWriter)
Dim alink As HyperLink = Me
If alink.NavigateUrl.StartsWith("http") Then
writer.RenderBeginTag(HtmlTextWriterTag.A)
writer.AddAttribute(HtmlTextWriterAttribute.Href, alink.NavigateUrl)
writer.AddAttribute(HtmlTextWriterAttribute.Title, alink.Text)
writer.AddAttribute(HtmlTextWriterAttribute.Value, alink.Text)
writer.AddAttribute(HtmlTextWriterAttribute.Target, "_blank")
writer.RenderEndTag()
End If
End Sub
End Class
答案 0 :(得分:1)
使用jQuery更容易,只需添加:
$(document).on('click', "a[href^='http']:not([href*='" + window.location.host + "'][target='_blank']),a[href^='//']:not([href*='" + window.location.host + "'][target='_blank']),a[href$='.pdf'],a[href$='.xls'],a[href$='.xlsx'],a[href$='.doc'],a[href$='.docx'],a[href$='.ppt'],a[href$='.pptx']", null, function () {
$(this).attr('target', '_blank');
});
答案 1 :(得分:0)
我在后面的代码中覆盖了Render方法。 http://forums.asp.net/t/509737.aspx?override+the+href+tag+of+a+LinkButton。 NewWindow这个页面上的代码给了我这个想法。