如何使用Jquery / JS根据URL更改Wordpress中的CSS属性?

我有一个使用WordPress,主题和滑块革命的网站。我想在移动菜单中使用超链接来根据URL更改字体。例如,如果用户当前在/ about上,则使用jquery / javascript更改css字体属性。

我想知道要使用什么JS / jquery代码以及如何添加到WordPress中?

抱歉,但是我是JS / jquery新手

谢谢

creamll 回答:如何使用Jquery / JS根据URL更改Wordpress中的CSS属性?

假设您有一个带有CSS类.menu的菜单,如下所示:

<ul class='menu'>
  <li><a href="index.html">Home</a></li>
  <li><a href="aboutus.html">About Us</a></li>
</ul>

要获取访问者的网页网址,请访问:

let currentURL = window.location.href.split('/').pop(); //Gets the url,splits it by / and return the last piece which is the URL you need.

然后将菜单链接保存在某个地方:

let menuLinks = document.querySelectorAll('.menu a');

这是一个NodeList,因此我们可以使用forEach来查看您需要的是哪一个:

menuLinks.forEach(link => {
  if(link.href.includes(currentURL)) {
    //You can add inline styling like this
    link.style.fontFamily = "Arial";
    link.style.fontWeight = "Bold";
    // Or you can add another class name to the element
    link.classList.add('currentUrlStyle'); // You need to style .currentUrlStyle selector in your css file.
  }
}

根据您的问题,类似的方法可能会解决您的问题。在Vanilla JS中。

本文链接:https://www.f2er.com/3167191.html

大家都在问