功能我有以下代碼:如何調用一個CSS類onclick事件
<title>Web Audio API</title>
</head>
<body>
<div class="post red">
<div class="inner">
<h2 class="title">Oscillator</h2>
<div class="post_info">
<p class="date"><i class="fa fa-audio-description"></i> Placeholder</p>
<p class="read_length"><i class="fa fa-clock-o"></i> 1 minute read</p>
<input id="red" type="range" min="50" max="5000" step="1" value="90"/>
</div>
</div>
</div>
</a>
<a href="#">
<div class="post green">
<div class="inner">
<h2 class="title">Filter</h2>
<div class="post_info">
<p class="date"><i class="fa fa-audio-description"></i> Placeholder</p>
<p class="read_length"><i class="fa fa-clock-o"></i> 1 minute read</p>
<input id="green" type="range" min="50" max="5000" step="1" value="90"/>
<input id="green" type="range" min="50" max="5000" step="1" value="90"/>
<script>
</script>
</div>
</div>
</div>
</a>
<a href="#">
<div class="post purple">
<div class="inner">
<h2 class="title">LFO</h2>
<div class="post_info">
<p class="date"><i class="fa fa-audio-description"></i> Placeholder</p>
<p class="read_length"><i class="fa fa-clock-o"></i> 1 minute read</p>
<input id="purple" type="range" min="50" max="5000" step="1" value="90"/>
<input id="purple" type="range" min="50" max="5000" step="1" value="90"/>
<input id="purple" type="range" min="50" max="5000" step="1" value="90"/>
</div>
</div>
</div>
</a>
<a href="#">
<div class="post lime_green">
<div class="inner">
<h2 class="title">VCO</h2>
<div class="post_info">
<p class="date"><i class="fa fa-audio-description"></i> Placeholder</p>
<p class="read_length"><i class="fa fa-clock-o"></i> 1 minute read</p>
</div>
</div>
</div>
</a>
<a href="#">
<div class="post pink">
<div class="inner">
<h2 class="title">Placeholder</h2>
<div class="post_info">
<p class="date"><i class="fa fa-audio-description"></i> Placeholder</p>
<p class="read_length"><i class="fa fa-clock-o"></i> 1 minute read</p>
</div>
</div>
</div>
</a>
<a href="#">
<div class="post orange">
<div class="inner">
<h2 class="title">Placeholder</h2>
<div class="post_info">
<p class="date"><i class="fa fa-audio-description"></i> Placeholder</p>
<p class="read_length"><i class="fa fa-clock-o"></i> 1 minute read</p>
</div>
</div>
</div>
</a>
<a href="#">
<div class="post yellow">
<div class="inner">
<h2 class="title">Placeholder</h2>
<div class="post_info">
<p class="date"><i class="fa fa-audio-description"></i> Placeholder</p>
<p class="read_length"><i class="fa fa-clock-o"></i> 1 minute read</p>
</div>
</div>
</div>
</a>
<a href="#">
<div class="post blue">
<div class="inner">
<h2 class="title">Placeholder</h2>
<div class="post_info">
<p class="date"><i class="fa fa-audio-description"></i> Placeholder</p>
<p class="read_length"><i class="fa fa-clock-o"></i> 1 minute read</p>
</div>
</div>
</div>
</a>
</body>
// JS
context = new AudioContext();
var osc = document.getElementByClassName('post red');
osc.onmousedown = function() {
var oscPitch = document.getElementByClassName('post red').value;
oscillator = context.createOscillator(),
oscillator.type = 0;
oscillator.frequency.value = oscPitch;
oscillator.connect(context.destination);
oscillator.start(osc);
};
osc.onmouseup = function() {
oscillator.disconnect();
};
我的嘗試是在CSS類單擊事件上運行js代碼。說:紅色
我不知道是否有人知道如何做到這一點。基本上它只是發揮振盪器的聲音。其餘的代碼並不重要。謝謝!
(1)它是'getElementsByClassName'(注意具有S的多個元件)。 (2)它返回一個節點列表,所以需要通過索引迭代或者只使用第一個。 – Abhitalks
感謝您的回答,但我已經研究過,似乎瀏覽器不需要節點列表中的項目來知道您指的是說父級CSS類元素。節點中的第一個。我錯了嗎? – eshalit
請閱讀此處:https://developer.mozilla.org/en/docs/Web/API/Document/getElementsByClassName – Abhitalks