こんにちは私はjavaScriptを学び、サイトで3つのボタンを使用して段落のフォントサイズを変更しようとしていますが、コードを書いているときにコンソールがこのエラーを出力し、それが何であるかを理解できません。私はまた、それをコーディングする方法が正しいかどうかを知りたいです。どうもありがとうございました。
htmlコード:
<!DOCTYPE html>
<html leng="es">
<head>
<meta charset="UTF-8">
<title>Mi ejercicio DHTML</title>
<link rel="stylesheet" type="text/css" href="css/estilos.css">
<script type="text/javascript" src="js/codigo.js"></script>
</head>
<body>
<p id="parrafo">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to.
</p>
<span class="boton" id="boton1">Fuente pequeña</span>
<span class="boton" id="boton2">Fuente mediana</span>
<span class="boton" id="boton3">Fuente grande</span>
</body>
</html>
css:
p {
font-size: 13px;
margin-bottom: 2em;
}
.boton {
background: red;
padding: .5em 1em;
}
javascript:
window.addEventListener('load', inicio, false);
function inicio() {
var indice = [1, 2, 3];
for (var i = 0; i < indice.length; i++) {
var boton = document.getElementById('boton' + indice[i]);
boton.addEventListener('click', cambiarFuente, false);
};
}
function cambiarFuente() {
console.log('hola');
}
そして最後にエラー:
Uncaught TypeError: Cannot call method 'addEventListener' of null codigo.js:6
注目してくれてありがとう:)
さて、私は前にタグを配置しました、そして今それは少なくとも私がそう思うもので動作しています....別の問題が発生し、段落がそのフォンサイズを変更しないということですが、それでも動作しているようにコンソールログメッセージを受け取ります:/
window.addEventListener('load', inicio, false);
function inicio(){
var indice =[1,2,3];
for (var i = 0; i < indice.length; i++) {
var boton = document.getElementById('boton' + indice[i]);
boton.addEventListener('click', cambiarFuente, false);
};
function cambiarFuente(){
var parrafo = document.getElementById('parrafo');
parrafo.style.fontSize=20;
console.log('hola');
}
}
また、各ボタンxDに異なるフォントサイズを割り当てる方法もわかりません。誰かが私に何かを提案できますか?