Javascript 我正在使用getElement,但它';为什么在我的代码中不起作用?
我正在尝试使用getElementById()更改“p”标记的颜色,但它不起作用 HTML:Javascript 我正在使用getElement,但它';为什么在我的代码中不起作用?,javascript,Javascript,我正在尝试使用getElementById()更改“p”标记的颜色,但它不起作用 HTML: id应该用引号括起来 document.getElementById("fon"); 如果要使用JS更改段落颜色。您可以使用以下代码:) 函数changecolor() { document.getElementById(“fon”).style.color='blue'; } 你好,这是一个段落 我刚刚学习了JavaScriptTypo。应该是document.getElementById('fo
id应该用引号括起来
document.getElementById("fon");
如果要使用JS更改段落颜色。您可以使用以下代码:)强>
函数changecolor()
{
document.getElementById(“fon”).style.color='blue';
}
你好,这是一个段落
我刚刚学习了JavaScriptTypo。应该是document.getElementById('fon')
document.getElementById(fon)代码>应该是document.getElementById(“fon”)代码>谢谢,我的手机没有正确的引号。没有注意到.worked,但是如果我使用getElementByClassName,也不要工作:var d=document.getElementByTagName('p');d.style.color='红色';您能帮助我吗?getElementByTagName()返回集合而不是元素本身。你可以从中读到更多。getElementByClassName()的工作方式与getElementByTagName相同,它返回一个HTMLcollection,如图所示。
var c = document.getElementById(fon);
c.style.color = 'blue';
document.getElementById("fon");
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
function changecolor()
{
document.getElementById("fon").style.color='blue';
}
</script>
</head>
<body>
<p id="fon" onclick="changecolor()"> hello this is a para</p>
</body>
</html>