简体   繁体   English

如何用javascript更改链接的颜色?

[英]How to change the color of the links with javascript?

I want to know how can I manipulate all the links on a page with javascript. 我想知道如何使用javascript操作页面上的所有链接。 I can get elements by id's with document.getElementById(id) , but how can I get the links? 我可以使用document.getElementById(id)通过id获取元素,但是如何获取链接? And also how can I get all elements with a certain classname ? 而且我如何获得具有某个classname所有元素? I want to change the color of the link and class elements. 我想改变链接和类元素的颜色。

I mean these links: 我的意思是这些链接:

<a href="http://www.google.com">This is a link</a>

And an example for an element with a class: 以及具有类的元素的示例:

<span class="link">This is an element with a class</span>

Please no jquery. 请不要jquery。 I want javascript. 我想要javascript。

Simple and straightforward (in pure JS too!) 简单明了(纯粹的JS也是如此!)

colorLinks("#00FF00");

function colorLinks(hex)
{
    var links = document.getElementsByTagName("a");
    for(var i=0;i<links.length;i++)
    {
        if(links[i].href)
        {
            links[i].style.color = hex;  
        }
    }  
}

If it's a class name you're looking for and you know the tag, just use this. 如果它是您正在寻找的类名,并且您知道该标记,请使用此标记。

var elements = document.getElementsByTagName("span");
for(var j=0;j<elements.length;j++)
{
    if(elements[j].className === "your class here")
    {
        //do something
    }  
}

You can also look at getElementsByClassName and querySelectorAll . 您还可以查看getElementsByClassNamequerySelectorAll Both have support in most new browsers. 两者都支持大多数新浏览器。

The pure-JavaScript version isn't all that complicated: 纯JavaScript版本并不复杂:

var elements = document.getElementsByTagName('a');

for (var i = 0; i < elements.length; i++) {
    if (elements.className.split(/\s+/).indexOf('red') !== -1) {
        elements[i].style.color = 'red';
    }
}

And for modern browsers: 对于现代浏览器:

var elements = document.querySelectorAll('a.red');

[].slice.call(elements).forEach(function(elem) {
    elem.style.color = 'red';
});

This is how I change all hyperlink colors (normal/hover): 这是我更改所有超链接颜色(正常/悬停)的方式:

function changeTextHyperlinkColours(inputColorNormal, inputColorHover) { 

    var sheets = document.styleSheets;
    var slen = sheets.length; 

    for(var i=0; i<slen; i++) { 

        var rules = document.styleSheets[i].cssRules; 
        var rlen = rules.length; 

        for(var j=0; j<rlen; j++) { 

            if (rules[j].selectorText == 'a') {
                rules[j].style['color'] = inputColorNormal;
            } 

            if (rules[j].selectorText == 'a:hover') {
                rules[j].style['color'] = inputColorHover;}
            }
        } 
    }
}

You can use document.getElementsByTagName("a") . 您可以使用document.getElementsByTagName("a") This function returns an array of the <a> elements in the page. 此函数返回页面中<a>元素的数组。 Loop over this array, and use .style.color = "#000000" in each element. 遍历此数组,并在每个元素中使用.style.color = "#000000"

您还可以在范围中嵌入链接文本并更改颜色

<a href='www.mydomain.com'><span onclick='this.style.color="red"'>Visit Us</span></a>

Update: I still recommend using jQuery, but, if you want to learn how to do it without, I would recommend heading over to this site. 更新:我仍然建议使用jQuery,但是,如果你想学习如何不用它,我建议你去这个网站。 This shows how to change link colors when you mouse over the link, but you can easily extrapolate for your specific situation: Javascript Change Link Text Color onmouseover 这显示了当您将鼠标悬停在链接上时如何更改链接颜色,但您可以轻松地根据您的具体情况进行推断: Javascript更改链接文本颜色onmouseover

-- -

Ottomanlast has a good point about checking out jQuery to help you out with this task (although it can be done without the use of a library). Ottomanlast有一个关于检查jQuery来帮助你完成这项任务的好处(尽管可以在不使用库的情况下完成)。 However, just so you have an example of what he is talking about, here is how you could change link colors using jQuery. 但是,就像你有一个他正在谈论的例子,这里是你如何使用jQuery更改链接颜色。

$('.linkClass').click(function(){
      $(this).css('color', 'green');
});

This example changes the color of a specific link when it is clicked. 此示例在单击时更改特定链接的颜色。

$('a').css('color', 'green');

This example will change all the links to a green color. 此示例将所有链接更改为绿色。

$('.linkClass').click(function(){
      $(this).removeClass('oldClass');
      $(this).addClass('newClass');
});

This does the same thing as the first example, but this removes and adds CSS classes that you already have defined elsewhere. 这与第一个示例的作用相同,但这会删除并添加您已在其他位置定义的CSS类。 (I would recommend this method over just editing the CSS directly.) (我建议这种方法直接编辑CSS。)

Anyway, the point I'm trying to make is that jQuery makes it extremely easy to select and then make changes to the objects within your HTML document. 无论如何,我想要的是,jQuery使得选择非常容易,然后对HTML文档中的对象进行更改。 You may want to take a look into it. 你可能想看看它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM