Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

How to click a button with event in JavaScript?

There is a website called https://freesound.org and I would like to press the play button with JavaScript.

I tried

document.getElementsByClassName("play toggle")[0].click();

and it works only if you have already clicked anywhere in the page. I also tried focus() on an element in the page but it will not work. Do I need to do this with an input event? How I could?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Media autoplay behavior is browser dependent. For instance, Chrome does not allow audio autoplay until the user has interacted with the page (click, tap, etc).

If you are only worried about autoplay in your browser, Chrome has a flag that you can set to allow audio to play without user interaction. If you are trying to create a script/extension/etc that will impact other users, you will need to research the behavior of the browser(s) you are targeting.

Edit: just saw that you are using Firefox Portable. Information about autoplay behavior can be found here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.7k users

...