Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我正在使用getElement,但它';为什么在我的代码中不起作用?_Javascript - Fatal编程技术网

Javascript 我正在使用getElement,但它';为什么在我的代码中不起作用?

Javascript 我正在使用getElement,但它';为什么在我的代码中不起作用?,javascript,Javascript,我正在尝试使用getElementById()更改“p”标记的颜色,但它不起作用 HTML: id应该用引号括起来 document.getElementById("fon"); 如果要使用JS更改段落颜色。您可以使用以下代码:) 函数changecolor() { document.getElementById(“fon”).style.color='blue'; } 你好,这是一个段落 我刚刚学习了JavaScriptTypo。应该是document.getElementById('fo

我正在尝试使用getElementById()更改“p”标记的颜色,但它不起作用

HTML:


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>