如何在 jquery 的 while 循环中调用点击函数?
how to call click function inside a while loop in jquery?
如何在 jquery 的 while 循环中调用点击函数?
$(document).ready(function(){
var count=2;
while(count>0){
$(".box1").click();
count--;
}
$(".box1").click(function(){
var aud=document.createElement('audio');
aud.src="tom-1.mp3";
aud.play();
});
});
- 您需要将
.box1
指定为 String
。
- 调用前附上
event handler
。
- 将
audio
添加到 HTML。 (请勿在 javascript 中使用 virtually),否则会出现 Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. . now I am getting this error.
错误。
$(document).ready(function() {
var count = 2;
$(".box1").click(function() {
var aud = document.getElementById('audio');
// replace with your audio
aud.src = "https://freesound.org/data/previews/515/515391_1453392-lq.mp3";
aud.play();
});
while (count > 0) {
$(".box1").click();
count--;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<audio id='audio'></audio>
<button class='box1'>Play</button>
如何在 jquery 的 while 循环中调用点击函数?
$(document).ready(function(){
var count=2;
while(count>0){
$(".box1").click();
count--;
}
$(".box1").click(function(){
var aud=document.createElement('audio');
aud.src="tom-1.mp3";
aud.play();
});
});
- 您需要将
.box1
指定为String
。 - 调用前附上
event handler
。 - 将
audio
添加到 HTML。 (请勿在 javascript 中使用 virtually),否则会出现Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. . now I am getting this error.
错误。
$(document).ready(function() {
var count = 2;
$(".box1").click(function() {
var aud = document.getElementById('audio');
// replace with your audio
aud.src = "https://freesound.org/data/previews/515/515391_1453392-lq.mp3";
aud.play();
});
while (count > 0) {
$(".box1").click();
count--;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<audio id='audio'></audio>
<button class='box1'>Play</button>