简体   繁体   English

在 html 中单击页面时尝试使链接看起来不同

[英]trying to make a link look different when page is clicked in html

well i have seen some similar questions but i couldn't find the solution i needed i am trying to make it so that when the user moves from one page to another in the website when the page is loaded the a tag should change color and stay that way i have tried using this code but it didnt work at all here is the html of the code好吧,我已经看到了一些类似的问题,但我找不到我需要的解决方案,我正在努力做到这一点,以便当用户在加载页面时从网站中的一个页面移动到另一个页面时,a 标签应该改变颜色并保持不变这样我就尝试过使用此代码,但它根本不起作用是代码的 html

 <div id="button-group" style="width:100%">
        <a href="#surprise"><img src="felix.png" id="cat" ></a>
        <a href="C:\xampp\htdocs\project\home.html" style="width:10%" class="button">home</a>
        <a href="C:\xampp\htdocs\project\Hobbies.html" id="hobbies" style="width:10%" class="button">Hobbies</a>
        <a href="#contact_us " style="width:10%" class="button">Contact</a>
        <a href="C:\xampp\htdocs\project\Services.html" style="width:10%" class="button">Services</a>
    </div>

so for example if i am on the Hobbies page and click the home page the home page should turn the color to blue or if i am on hobbies and its clicked it should also change color onload here is the jquery that i tried因此,例如,如果我在爱好页面上并单击主页,则主页应该将颜色变为蓝色,或者如果我在爱好上并且单击它也应该改变颜色 onload 这里是我试过的 jquery

$(document).ready(function(){
    $("#hobbies").trigger('.click');
});

and here is the css when triggered这是触发时的 css

.a :click {
  background-color:rgba(0, 183, 255, 0.788);
  }

You can use sessionStorage to store the link that was clicked and get it when the new page is loaded to set a class to the last clicked navigation element.您可以使用sessionStorage存储单击的链接,并在加载新页面时获取它,以将 class 设置为最后单击的导航元素。

 $(document).ready(function() {
    let clicked = $.trim(sessionStorage.getItem("clicked"));
    if (clicked) {
       $("#button-group a").each(function() {
          if ($.trim($(this).attr("href")) == clicked) {
             $(this).addClass("clicked");
          };
       });
    }
    $("#button-group a").on("click", function() {
       $("#button-group a").each(function() {
          $(this).removeClass("clicked");
       });
       let link = $(this).attr("href");
       $(this).addClass("clicked");
       sessionStorage.setItem("clicked", link);
    });
 });

As a stack snippet for this doesn't work on Stackoverflow, here's a Fiddle with the adjustment to use preventDefault() on click of a link.由于此堆栈片段在 Stackoverflow 上不起作用,这里有一个Fiddle调整以在单击链接时使用preventDefault() Just run the Fiddle again after clicking on a link to see that the last clicked link gets added the class clicked .单击链接后再次运行 Fiddle 以查看最后单击的链接已添加 class clicked

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当在另一个页面上单击链接时,试图使这个WordPress主题滚动到一个id - Trying to get this wordpress theme to scroll to an id when a link is clicked on a different page (html/css/javascript) 尝试将导航栏中的当前页面链接设为不同颜色 - (html/css/javascript) Trying to make my Current Page link in the navbar a different color 当在另一个页面上单击按钮时,试图在 html 页面上执行操作 - Trying to preform an act on an html page when a button is clicked on another page 单击链接到同一页面的不同部分时,禁用URL更改 - disable url change when link to a different part of the same page is clicked HTML CSS - 单击时使图标更改颜色(如链接) - HTML CSS - Make icon change color when clicked (like a link) 当单击JavaScript和HTML时如何使链接打开新窗口? - How to make a link open a new window when clicked in JavaScript and HTML? 在不同的 html 页面上单击链接后,如何更改 html 页面的 the.innerHTML? - how do i change the .innerHTML of a html page after a link has been clicked on a different html page? <Link>单击时冻结页面 - <Link> freezing page when clicked 如何使HTML标题看起来像链接 - How to make a HTML heading look like a link 需要帮助编辑JavaScript-尝试在单击另一个链接时使该链接“处于活动状态” - Need help editing JavaScript - Trying to make a link “active” when another link is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM