简体   繁体   中英

javascript element.click() issue Safari

On click of 'subButton2' button, 'uploadBtn'(display:none;) is clicked.

According to this it should be cross browser:

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_html_click

Here is my code that works in chrome, opera and firefox but fails in safari:

var page = window.location.toString();
if(page.indexOf('register') !== -1){
    var subButton2 = document.getElementById("subButton2");
    subButton2.onclick = function () {
        var uploadBtn = document.getElementById("fileBtnHide");
        //THIS FAILS IN SAFARI IF uploadBtn style= display:none;
        uploadBtn.click();
    };
 };

***Only javascript, no jquery solutions, please

It's running on: http://monkey-me.herokuapp.com/register

The Photo button is the one that triggers the second button.

Instead of setting it to display none (which safari might interpret as disabled) hide it with some other css properties.

#fileBtnHide {
    height: 0;
    width: 0;
    opacity: 0;
    position: absolute;
}

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