如何用javascript改变链接的颜色?
我想知道如何使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
简单明了(在纯 JS 中也是如此!)
如果您要查找的是类名并且您知道标签,则只需使用它即可。
您还可以查看 getElementsByClassName 和 querySelectorAll。大多数新浏览器都支持两者。
Simple and straightforward (in pure JS too!)
If it's a class name you're looking for and you know the tag, just use this.
You can also look at getElementsByClassName and querySelectorAll. Both have support in most new browsers.
纯 JavaScript 版本并不那么复杂:
对于现代浏览器:
The pure-JavaScript version isn't all that complicated:
And for modern browsers:
更新:我仍然建议使用 jQuery,但是,如果您想了解如何不使用 jQuery,我建议您访问此网站。这显示了当您将鼠标悬停在链接上时如何更改链接颜色,但您可以轻松推断您的具体情况:Javascript 更改 onmouseover 链接文本颜色
--
Ottomanlast 有一个关于查看 jQuery 来帮助您完成此任务(尽管它可以在不使用库的情况下完成)。然而,为了让您有一个他正在谈论的内容的示例,以下是您如何使用 jQuery 更改链接颜色。
此示例在单击特定链接时更改该链接的颜色。
此示例会将所有链接更改为绿色。
这与第一个示例执行相同的操作,但是这会删除并添加您已在其他地方定义的 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.
This example changes the color of a specific link when it is clicked.
This example will change all the links to a green color.
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.
您可以使用
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.这就是我更改所有超链接颜色(正常/悬停)的方法:
This is how I change all hyperlink colors (normal/hover):
您还可以在范围中嵌入链接文本并更改颜色
Also you can embed the link text in the span and change the color