【发布时间】:2014-07-27 21:20:06
【问题描述】:
我有一个覆盖链接行为的 jQuery 插件,以允许 Ajax 加载页面内容。使用像 $(document).on('click','a', function(){}); 这样的委托事件就足够简单了。
但我只希望它适用于与这些不同的链接(Ajax 加载不适用于它们,因此此类链接需要正常运行):
target="_blank" // New browser window
target="_self" // Force replacement of current window (specific to my plugins)
href="#..." // Bookmark link (page is already loaded).
href="afs://..." // AFS file access.
href="cid://..." // Content identifiers for MIME body part.
href="file://..." // Specifies the address of a file from the locally accessible drive.
href="ftp://..." // Uses Internet File Transfer Protocol (FTP) to retrieve a file.
href="http://..." // The most commonly used access method.
href="https://..." // Provide some level of security of transmission
href="mailto://..." // Opens an email program.
href="mid://..." // The message identifier for email.
href="news://..." // Usenet newsgroup.
href="x-exec://..." // Executable program.
href="http://AnythingNotHere.com" // External links
示例代码:
$(document).on('click', 'a:not([target="_blank"])', function(){
var $this = $(this);
if ('some additional check of href'){
// Do ajax load and stop default behaviour
return false;
}
// allow link to work normally
});
问:
有没有一种方法可以轻松检测所有只能在当前网站内导航的“本地链接”?不包括上述所有变体。
注意:这是针对 MVC 5 Razor 网站,因此不太可能出现绝对网站 URL。
【问题讨论】:
-
为什么要投反对票?我认为这个结构良好的问题没有任何问题。
-
Down-voter(s) (复数现在)... 有用的 cmets 好吗?这是一个完全有效的问题。
-
我可能会使用选择器
a:not([href*="://"]),然后将其与其他选择器结合使用。单个选择器可能是不可能的。 -
@Quentin:我已经看到了这个问题,这不是重复的(不完全相同的问题)。请阅读具体细节。
-
在什么方面不重复?能够将链接识别为外部链接以添加类与能够识别它们以将它们从您的选择中排除是相同的。
标签: javascript jquery html regex