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



我试图使用getElementById((更改"p"标记的颜色,但不起作用。。。

HTML:

<p id="fon">changing color</p>

JavaScript:

var c = document.getElementById(fon);
c.style.color = 'blue';

id应使用引号

document.getElementById("fon");

如果您想使用JS更改段落颜色。您可以使用此代码..:(

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

最新更新