我有一个使用 SSI 的菜单,问题是标签需要一个 id 为“here”的 CSS 来应用背景图像来显示最终用户在网站上的位置。
那么有没有办法获取浏览器当前的 url(即:mypage.html),使用 .find() 函数选择 anchor 标记并添加 id="here"?
我可以在概念上看到它的工作原理,但无论我尝试什么,我似乎都无法让 jQuery 做到这一点。
最佳答案
取决于它是否是相对 URL,例如
$("#yourmenu")
.find("a[href$=" + window.location.pathname + "]:first").attr("id", "here")
可能有效。这会查找 a
标记,该标记的 href
以当前路径名结尾;这对您来说可能限制不够。
关于jQuery - 帮助 .find() 并将 id 添加到 anchor 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2395353/