JavaScript函数-更改文本的颜色 [英] JavaScript Function - Change the color of text
问题描述
我必须创建一个名为tousVerts(法语中的 everythingGreen)的函数,该函数会将页面上需要的所有文本的颜色更改为绿色。该文本位于html代码中的EM或A标签之间,因此我尝试使用document.getElementByTagName()来获取它。这些标记之一必须传递函数的参数,然后更改颜色。
I have to make a function called tousVerts ("everythingGreen" in French) that will change the color of all the text I need on the page to green. This text is between tags EM or A in html-code, so I try to get to it with document.getElementByTagName(). One of these tags must pass through the parameters of function and then change the color.
var tousVerts = function(elt) {
var tousVerts = document.getElementByTagName (elt);
tousVerts[0];
tousVerts[0].style.color = "green";}
我看到了一定是简单的东西,但它不起作用,似乎我必须使用本地i来更改方括号之间的数字,但我不确定。
I see it has to be something simple but it doesn't work and it seems like I've to use a local i to change the numbers between brackets but I'm not sure.
推荐答案
您需要使用 document.getElementsByTagName
因为它返回多个元素:
You need to use document.getElementsByTagName
since it returns multiple elements:
var tousVerts = document.getElementsByTagName (elt);
此外, tousVerts [0];
是不需要:
var tousVerts = function(elt) {
var tousVerts = document.getElementsByTagName (elt);
tousVerts[0].style.color = "green";}
这篇关于JavaScript函数-更改文本的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!