最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - document.click() on a document - Stack Overflow

programmeradmin3浏览0评论

DOM button is getting clicked with .click() method, but its not working on document itself.

/* Working fine */
function getAlert() {
    alert("Clicked!!!");
}
var btn = document.getElementById('btn');
btn.onclick = getAlert;
btn.click()

/* Should be work  */
document.onclick = getAlert;
document.clicked()
<button id="btn">Click Me!</button>

DOM button is getting clicked with .click() method, but its not working on document itself.

/* Working fine */
function getAlert() {
    alert("Clicked!!!");
}
var btn = document.getElementById('btn');
btn.onclick = getAlert;
btn.click()

/* Should be work  */
document.onclick = getAlert;
document.clicked()
<button id="btn">Click Me!</button>

Please help.

Share Improve this question edited Jan 8, 2021 at 18:55 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Aug 6, 2018 at 11:00 Sudhakar LahaneSudhakar Lahane 1491 gold badge2 silver badges13 bronze badges 2
  • document.getElementById('btn'); this id is not found in the document. – Jai Commented Aug 6, 2018 at 11:02
  • 1 Your attempt at providing a minimal reproducible example demonstrates a number of problems which have nothing to do with the question you are asking. Please test your MCVEs to make sure they are a suitable demonstration of the actual problem you are having. – Quentin Commented Aug 6, 2018 at 11:03
Add a comment  | 

4 Answers 4

Reset to default 7

document objects are not things that can be clicked on.

Event handlers can be bound to a document, and events will bubble until they reach the document and trigger a handler, but you can't click directly on the document.

document.getElementById('btn') this id is not found in the document, So please add the element id and remove document.clicked() because its not a function on document object.

Here you may please check the working code snippet.

/* Working fine */
function getAlert() {
    alert("Clicked!!!");
}
var btn = document.getElementById('btn');
btn.onclick = getAlert;
btn.click()

/* Should be work  */
document.onclick = getAlert;
//document.clicked()
<button id="btn">Click Me!</button>

As Quentin already explains in his answer, a document object cannot be clicked upon.

Event handlers associated on document execute due to the events bubbling up the DOM. For example, a click event on the button in your snippet traverses the button -> then body -> then document.

So, as an alternative to document.clicked() you attempt in your question, you can trigger the event on body element via, document.body.click().

/* Working fine */
function getAlert() {
    alert("Clicked!!!");
}
var btn = document.getElementById('btn');
btn.onclick = getAlert;
btn.click()

/* Should be work  */
document.onclick = getAlert;
document.body.click()
<button id="btn">Click Me!</button>

You can read more about event bubbling here: What is event bubbling and capturing?


Note: I have added ID attribute to the button in your snippet to ensure that it runs.

you can do this with jquery like this:

$(document).on('click', ()=>{console.log('clicked')})

$(document).click()
发布评论

评论列表(0)

  1. 暂无评论