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

发布于 2024-10-19 22:47:24 字数 437 浏览 1 评论 0原文

我想知道如何使用 javascript 操作页面上的所有链接。我可以使用 document.getElementById(id) 按 id 获取元素,但如何获取链接?另外,如何获取具有特定classname的所有元素?我想更改链接和类元素的颜色。

我的意思是这些链接:

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

以及带有类的元素的示例:

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

请不要使用 jquery。 我想要 javascript。

I want to know how can I manipulate all the links on a page with javascript. I can get elements by id's with document.getElementById(id), but how can I get the links? And also how can I get all elements with a certain 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. I want javascript.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

苄①跕圉湢 2024-10-26 22:47:24

简单明了(在纯 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;  
        }
    }  
}

如果您要查找的是类名并且您知道标签,则只需使用它即可。

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

您还可以查看 getElementsByClassNamequerySelectorAll。大多数新浏览器都支持两者。

Simple and straightforward (in pure JS too!)

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. Both have support in most new browsers.

哀由 2024-10-26 22:47:24

纯 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';
    }
}

对于现代浏览器:

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

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

The pure-JavaScript version isn't all that complicated:

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';
});
〗斷ホ乔殘χμё〖 2024-10-26 22:47:24

更新:我仍然建议使用 jQuery,但是,如果您想了解如何不使用 jQuery,我建议您访问此网站。这显示了当您将鼠标悬停在链接上时如何更改链接颜色,但您可以轻松推断您的具体情况:Javascript 更改 onmouseover 链接文本颜色

--

Ottomanlast 有一个关于查看 jQuery 来帮助您完成此任务(尽管它可以在不使用库的情况下完成)。然而,为了让您有一个他正在谈论的内容的示例,以下是您如何使用 jQuery 更改链接颜色。

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

此示例在单击特定链接时更改该链接的颜色。

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

此示例会将所有链接更改为绿色。

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

这与第一个示例执行相同的操作,但是这会删除并添加您已在其他地方定义的 CSS 类。 (我会推荐这种方法,而不是直接编辑 CSS。)

无论如何,我想要表达的观点是 jQuery 使得选择和更改 HTML 文档中的对象变得非常容易。您可能想查看一下。

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. 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

--

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). However, just so you have an example of what he is talking about, here is how you could change link colors using 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. (I would recommend this method over just editing the CSS directly.)

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. You may want to take a look into it.

断念 2024-10-26 22:47:24

您可以使用document.getElementsByTagName("a")。此函数返回页面中 元素的数组。循环此数组,并在每个元素中使用 .style.color = "#000000"

You can use document.getElementsByTagName("a"). This function returns an array of the <a> elements in the page. Loop over this array, and use .style.color = "#000000" in each element.

心奴独伤 2024-10-26 22:47:24

这就是我更改所有超链接颜色(正常/悬停)的方法:

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;}
            }
        } 
    }
}

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;}
            }
        } 
    }
}
筱武穆 2024-10-26 22:47:24

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

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

Also you can embed the link text in the span and change the color

<a href='www.mydomain.com'><span onclick='this.style.color="red"'>Visit Us</span></a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文