如果该链接链接到当前页面,则定位该链接? [英] Target a link if that link links to the current page?
问题描述
这可能是一个愚蠢的问题,因为我似乎找不到答案:P
this may be a stupid question as I can't seem to find an answer :P
使用javascript/jQuery是否只有在链接到当前页面的情况下才能定位页面上的所有链接?
Is there a way with javascript/jQuery to Target all links on a page ONLY if they link to the current page?
说,出于意图和目的,我在所有页面上都有一个静态边栏:
Say i've got a static sidebar on ALL pages, for intents and purposes:
<ul id="sidebar">
<li><a href="/one">One</a></li>
<li><a href="/two">Two</a></li>
<li><a href="/three">Three</a></li>
</ul>
请注意,所有代码均相同.假设我在"www.domain.com/two"上-是否可以定位
Notice the code for ALL of them is the same. Let's say I'm on "www.domain.com/two" - Is there a way to target
<li><a href="#">Two</a></li>
因为它链接到当前页面?
because it's linking to the current page?
*** ANSWERED * ** * *
***ANSWERED*****
那个人删除了他的答案-但我用它来创建它-
The guy deleted his answer - but I used it to create this -
var linksToCurrentPage = $('a[href="' + window.location.href + '"]');
if (linksToCurrentPage) {
$('a').addClass('currently-active');
};
有效的方法:)
推荐答案
尝试以下操作:
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<ul id="sidebar">
<li><a href="/one">One</a></li>
<li><a href="/two">Two</a></li>
<li><a href="/three">Three</a></li>
</ul>
<script>
var pathname = window.location.pathname;
//$("a:contains('One')").css("color", "red"); // This will make One red (notice capital O)
$("a:contains(pathname)").css("color", "red");
</script>
这篇关于如果该链接链接到当前页面,则定位该链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!