我想用他们相应的声音来突出显示特定的para/list。
responsivevoice.js中有没有回调。我以回调function.But的身份连接,但它不工作。
每当我将控制台替换为高亮显示时,它只生成一个而不是三个。
我认为onend仅在第一个para .But之后调用,它应该适用于所有para/ul
请帮帮我..
我的代码:-
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="http://code.responsivevoice.org/develop/1.5.2/responsivevoice.js"></script>
<script>
var currentDataValue =0;
$( document ).ready(function(){
$("#1").click(function(){
$("p,ul").each(function( index, element){
responsiveVoice.speak($(this).text(),$('UK English Female').val(),{
pitch: .7,
onend: function() {
$(this).css( "backgroundColor", "yellow" );
}
});
});
});
});
$( document ).ready(function(){
$("#2").click(function(){
responsiveVoice.pause();
});
});
$( document ).ready(function(){
$("#3").click(function(){
responsiveVoice.resume();
});
});
$(window).load(function(){
$("#4").click(function(){
responsiveVoice.cancel();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<ul>
<li>this unoder list </li>
</ul>
<p id="test">This is another paragraph.</p>
<button id="1">start</button>
<button id="2">pause</button>
<button id="3">resume</button>
<button id="4">stop</button>
</body>
</html>发布于 2017-06-18 18:47:26
这个ResponsiveVoice是一个有bug的商业库,它为回调设置了一个变量,所以它只调用最后配置的回调,而不是所有的回调。当然,您可以修改库,也可以像这样逐个调用项目:
$("#1").click(function(){
var elements = $("p, ul");
speak = function(i, elements) {
responsiveVoice.speak($(elements[i]).text(),$('UK English Female').val(),{
pitch: .7,
onend: function() {
$(elements[i]).css("backgroundColor", "yellow");
if (i < elements.length) {
speak(i + 1, elements);
}
}
});
};
speak(0, elements);
});我会简单地使用Chrome API,如下所示:
var currentDataValue =0;
$( document ).ready(function(){
$("#1").click(function(){
var voice = speechSynthesis.getVoices().filter(function(voice){return voice.name == 'Allison'; })[0];
$("p,ul").each(function(index, element) {
u = new SpeechSynthesisUtterance($(this).text());
u.voice = voice;
u.onend = (function() {
console.log("Here");
$(this).css( "background-color", "yellow" );
}).bind($(this));
speechSynthesis.speak(u);
});
});
});https://stackoverflow.com/questions/44255187
复制相似问题