ここには 3 つあります
<span>a</span><span>b</span><span>c</span>
ディスカッションへの返信 (解決策)
完全なコード:
var div = document.getElementById('div');var span = div.getElementsByTagName('span');for(var i = 0; i < span.length; i++){ span[i].onclick = function(){ if(this.className.length){ this.className = ''; } else { this.className = 'red'; } }}
test <span>a</span><span>b</span><span>c</span><script> (function(){ var div = document.getElementById('div'); var span = div.getElementsByTagName('span'); for(var i = 0; i < span.length; i++){ span[i].onclick = function(){ if(this.className.length){ this.className = ''; } else { this.className = 'red'; } } } }());</script>
完全なコード:
var div = document.getElementById('div');var span = div.getElementsByTagName('span');for(var i = 0; i < span.length; i++){ span[i].onclick = function(){ if(this.className.length){ this.className = ''; } else { this.className = 'red'; } }}