如何从ID更改为班级?

时间:2019-01-25 18:36:37

标签: javascript html css

我正在尝试在Codepen https://codepen.io/hkdc/pen/RoJwVX上更改此代码,以使按钮具有类而不是ID。我该怎么做?

例如,我尝试将HTML文件中的id="play"更改为class="play",将CSS文件中的#play更改为.play,并将{{1} }到js文件中的#play-没有成功。

HTML:

.play

Css:

<div class=buttons>
  <button id=play></button> &nbsp;
  <button id=pause></button> &nbsp;
  <button id=stop></button>
</div>

JavaScript:

#play {
    background-image: url(https://rpsthecoder.github.io/js-speech-synthesis/play.svg);
}

#play.played {
    background-image: url(https://rpsthecoder.github.io/js-speech-synthesis/play1.svg);
}

#pause {
    background-image: url(https://rpsthecoder.github.io/js-speech-synthesis/pause.svg);
}

#pause.paused {
    background-image: url(https://rpsthecoder.github.io/js-speech-synthesis/pause1.svg);
}

#stop {
    background-image: url(https://rpsthecoder.github.io/js-speech-synthesis/stop.svg);
}

#stop.stopped {
    background-image: url(https://rpsthecoder.github.io/js-speech-synthesis/stop1.svg);
}

1 个答案:

答案 0 :(得分:1)

它对您不起作用,因为您没有仔细观察代码。代码清除了 className ,即play.className = '',而您必须使用 HTMLelement classList 属性。希望对您有帮助。

onload = function() {
	if ('speechSynthesis' in window) with(speechSynthesis) {

		var playEle = document.querySelector('.play');
		var pauseEle = document.querySelector('.pause');
		var stopEle = document.querySelector('.stop');
		var flag = false;

		playEle.addEventListener('click', onClickPlay);
		pauseEle.addEventListener('click', onClickPause);
		stopEle.addEventListener('click', onClickStop);

		function onClickPlay() {
			if (!flag) {
				flag = true;
				utterance = new SpeechSynthesisUtterance(document.querySelector('article').textContent);
				utterance.voice = getVoices()[0];
				utterance.onend = function() {
					flag = false;
					playEle.classList.remove('played');
					stopEle.classList.add('stopped');
				};
				playEle.classList.add('played');
				stopEle.classList.remove('stopped');
				speak(utterance);
			}
			if (paused) { /* unpause/resume narration */
				playEle.classList.add('played');
				pauseEle.classList.remove('pasused');
				resume();
			}
		}

		function onClickPause() {
			if (speaking && !paused) { /* pause narration */
				pauseEle.classList.add('paused');
				playEle.classList.remove('played');
				pause();
			}
		}

		function onClickStop() {
			if (speaking) { /* stop narration */
				/* for safari */
				stopEle.classList.add('stopped');
				playEle.classList.remove('played');
        pauseEle.classList.remove('paused');
				flag = false;
				cancel();

			}
		}

	}

	else { /* speech synthesis not supported */
		msg = document.createElement('h5');
		msg.textContent = "Detected no support for Speech Synthesis";
		msg.style.textAlign = 'center';
		msg.style.backgroundColor = 'red';
		msg.style.color = 'white';
		msg.style.marginTop = msg.style.marginBottom = 0;
		document.body.insertBefore(msg, document.querySelector('div'));
	}

}
@import url('https://fonts.googleapis.com/css?family=Crimson+Text');
html {
	font-size: 16pt;
	font-family: crimson text;
}

article {
	width: 60%;
	margin: auto;
}

blockquote,
div,
h1 {
	text-align: center;
}

blockquote {
	font-size: 1.2rem;
}

.buttons {
	margin-top: 25px;
}

button {
	background: none;
	border: none;
	cursor: pointer;
	height: 48px;
	outline: none;
	padding: 0;
	width: 48px;
}

.play {
	background-image: url(https://rpsthecoder.github.io/js-speech-synthesis/play.svg);
}

.play.played {
	background-image: url(https://rpsthecoder.github.io/js-speech-synthesis/play1.svg);
}

.pause {
	background-image: url(https://rpsthecoder.github.io/js-speech-synthesis/pause.svg);
}

.pause.paused {
	background-image: url(https://rpsthecoder.github.io/js-speech-synthesis/pause1.svg);
}

.stop {
	background-image: url(https://rpsthecoder.github.io/js-speech-synthesis/stop.svg);
}

.stop.stopped {
	background-image: url(https://rpsthecoder.github.io/js-speech-synthesis/stop1.svg);
}

@media (max-width: 700px) {
	html {
		font-size: 14pt;
	}
	article {
		width: 90%;
	}
}

body {
	margin: 0;
}


/*  demo decor */

#-demo-title {
	position: fixed;
	top: 0;
	width: 100%;
	height: 36px;
	background-color: #D5FFD1;
	color: #0F316C;
	line-height: 36px;
	font-weight: bold;
	font-size: 16pt;
	text-align: center;
}

#-demo-profile {
	float: left;
	height: 100%;
	opacity: .4;
}

#-demo-profile:hover {
	opacity: 1;
}

@media (max-width: 400px) {
	#-demo-title {
		font-size: 12pt;
	}
}
<div class=buttons>
	<button class=play></button> &nbsp;
	<button class=pause></button> &nbsp;
	<button class=stop></button>
</div>
<article>
	<h1>The Hare With Many Friends</h1>
	<img src="https://rpsthecoder.github.io/js-speech-synthesis/hare-and-friends.jpg" width="100%" style="margin: auto;filter: brightness(1.2);">
	<p>A hare was very popular with the other beasts who all claimed to be her friends. But one day she heard the hounds approaching and hoped to escape them by the aid of her many friends. So she went to the horse, and asked him to carry her away from the hounds on his back.</p>
	<p>But he declined, stating that he had important work to do for his master. “He felt sure,” he said, “that all her other friends would come to her assistance.” She then applied to the bull, and hoped that he would repel the hounds with his horns. The bull
		replied: “I am very sorry, but I have an appointment with a lady; but I feel sure that our friend the goat will do what you want.” </p>
	<p>The goat, however, feared that his back might do her some harm if he took her upon it. The ram, he felt sure, was the proper friend to apply to. So she went to the ram and told him the case. </p>
	<p>The ram replied: “Another time, my dear friend. I do not like to interfere on the present occasion, as hounds have been known to eat sheep as well as hares.” The Hare then applied, as a last hope, to the calf, who regretted that he was unable to help
		her, as he did not like to take the responsibility upon himself, as so many older persons than himself had declined the task. By this time the hounds were quite near, and the Hare took to her heels and luckily escaped.</p>
	<blockquote>Moral of the story: He that has many friends, has no friends.</blockquote>
</article>