質問

For some reason, I cannot use jquery.

With plain javascript, how could I simulate the link-click action on a web page.

Thanks.

Below is the javascript I tried to get online, but it seems not work.

function create_window (title, content){

    var comment = document.getElementById('modal'); 
alert(comment.innerHTML);
    if (comment.click) {
        alert('hi');
        comment.click();
    } else if (document.createEvent) {
        alert('3'); 
        var ev = document.createEvent('MouseEvents');
         ev.initMouseEvent('click', true, true, window, 1, 12, 345, 7, 220, false, false, true, false, 0, null )
         comment.dispatchEvent(ev);
    } 

}
役に立ちましたか?

解決

function simulateClick() {
  var evt = document.createEvent("MouseEvents");
  evt.initMouseEvent("click", true, true, window,
    0, 0, 0, 0, 0, false, false, false, false, 0, null);
  var cb = document.getElementById("link");
  var canceled = !cb.dispatchEvent(evt);
}

from here. Here is a fiddle, in which I define the function to create and dispatch the click event, add an event handler to the element, and call the function after a delay of 1 second.

他のヒント

Try this

<a href="doc.pdf"><span id="spnDownload">Download!</span></a>

$("#spnDownload").click();

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top