简体   繁体   中英

Manipulating elements on the page with javascript

Is it possible to write a javascript script clicking a button on a webpage that is not embedded in html? It is written in javascript and implemented using script src.

If I understand the question correctly, then the button is not present in the HTML markup initially, but is added to the markup using javascript? In this case, you need to write your script, which will be executed after the button is added, and everything will work.

Yes, it is possible to click a button that was created dynamically on the page, as long as the creation of the button come before your script.

For example, a button created like that:

var button = document.createElement('button');
button.innerHTML = 'newButton';
button.id = 'buttonId';
document.querySelector('body').appendChild(button);
button.addEventListener('click', function(e){
    alert('Button clicked');
});

Can be accessed by your script with this code:

document.querySelector('#buttonId').click();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM