How do you toggle links to stay a certain color until clicked again

一世执手 提交于 2019-12-11 07:55:46

问题


I'm trying to make it so that when I click on a link, it makes it stay a certain thing, and then when clicked again, change back.

How do I go about doing this? Can i do it in html + css? or i need js?


回答1:


You can do this with CSS + jQuery:

CSS:

a{
    color: blue
}
a.clicked{
    color: red;
}

jQuery:

$(document).ready(function(){
    $('a').click(function(){
        $(this).toggleClass('clicked');
    });
});

You can check an example here »




回答2:


If only looking for pure Javascript and HTML:

function toggle_link(select){
    var color = select.style.color;
    select.style.color = (color == "blue" ? "green" : "blue");
}

And in your HTML, use the onclick attribute.

<a onclick="toggle_link(this)" style="color:blue">Click to change color!</a>

A working fiddle: jsFiddle




回答3:


You'll definitely need JavaScript for that. (If you want to 'cheat' create 2 HTML pages with the same content except the links, so you can link them together but with a different name.)



来源:https://stackoverflow.com/questions/6712672/how-do-you-toggle-links-to-stay-a-certain-color-until-clicked-again

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!