[英]How can i add a class to an <a> with a specific href in php?
var url = window.location.pathname;
var filename = url.substring(url.lastIndexOf('/')+1);
$("ul").find("a[href=filename]").addClass('active');
在我的header.php中使用ul:
<li><a href="index.php">Főoldal</a></li>
<li><a href="gallery.php">Galéria</a></li>
<li><a href="games.php">Játékok</a></li>
<li><a href="contacts.php">Elérhetőségeink</a></li>
<li><a href= "about.php">Rólunk</a></li>
我花了幾個小時。 根據我當前所在的站點,我只是無法向錨標簽添加類。
嘗試這個:
$uri=$_SERVER['REQUEST_URI'];
這樣,您將獲得URI,例如“ /index.php ”,現在需要將其與標記中的url進行比較。 即使我建議使用JS / jQuery,例如:
var current_path_url=window.location.pathname;
$('a[href="'+current_path_url+'"]').parent().addClass('active');
變量不會在帶引號的字符串內擴展,您必須使用串聯(除非您想使用ES6模板文字,但是較舊的瀏覽器不支持它們)。
$("ul > li > a[href=" + filename + "]").addClass('active');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.