我很少使用 JS,无法弄清楚为什么“play-stop-toggle”中的文本没有显示。
我想要的结果是:
- [已解决] 中的文本在您单击它时和
<div id="play-stop-toggle" onclick="togglePlay()">Play</div>
之间切换,PLAY
并且STOP
- 相应地,
Tone.Transport.stop()
应该Tone.Transport.start()
切换以使声音var filter = new Tone.Filter
停止/开始播放。
目前,文本只会在第一次点击时从播放切换到停止,但不会切换回来。而且从来没有声音出来。
function togglePlay() {
Tone.Transport.toggle();
const status = Tone.Transport.state; // Is either 'started', 'stopped' or 'paused'
const element = document.getElementById("play-stop-toggle");
if (status == "started") {
element.innerText = "STOP";
Tone.Transport.stop()
} else {
element.innerText = "PLAY";
Tone.Transport.start()
}
}
var filter = new Tone.Filter({
type: 'lowpass',
Q: 12
}).toMaster()
body,
html {
height: 100%;
margin: 5em;
background-color: black;
}
h1 {
font-size: 2.1rem;
/* text-align: center !important; */
font-family: 'Courier New', Courier, monospace;
margin: 0px;
color: ghostwhite;
padding-bottom: 20px;
font-weight: bold;
text-align: justify;
}
p {
margin: 0;
font-size: 2rem;
font-family: 'Courier New', Courier, monospace;
color: ghostwhite;
padding-bottom: 20px;
text-align: justify;
font-weight: bold;
}
p.sig {
margin: 0;
font-size: 2rem;
font-family: 'Courier New', Courier, monospace;
color: ghostwhite;
padding-bottom: 20px;
text-align: right;
}
div.bodycontent {
margin: 0px;
padding: 0px;
width: 375px;
position: absolute;
top: 50%;
left: 50%;
margin-right: -50%;
transform: translate(-50%, -50%);
display: none;
}
a:link,
a:visited {
/* background-color: #f44336; */
color: white;
text-align: center;
font-style: italic;
text-decoration: underline;
display: inline-block;
cursor: pointer;
}
a:hover,
a:active {
background-color: white;
color: black;
cursor: pointer;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://unpkg.com/tone@13.8.25/build/Tone.js"></script>
<script src="p1-fish.js"></script>
<link rel="stylesheet" type="text/css" href="ristyle.css">
</head>
<body>
<div class="bg">
<h1>Heading</h1>
<p>Text</p>
</div>
<div id="play-stop-toggle" onclick="togglePlay()">Play</div>
</body>
</html>