简体   繁体   English

模拟<a href='dd'>使用JavaScript</a>的link( <a href='dd'>)点击</a>

[英]simulate a link(<a href='dd'>) click with javascript

For some reason, I cannot use jquery. 由于某种原因,我无法使用jquery。

With plain javascript, how could I simulate the link-click action on a web page. 使用普通的javascript,如何模拟网页上的链接单击操作。

Thanks. 谢谢。

Below is the javascript I tried to get online, but it seems not work. 以下是我尝试上网的JavaScript,但似乎无法正常工作。

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. 是一个小提琴,在其中我定义了用于创建和调度click事件,向该元素添加事件处理程序并在延迟1秒后调用该函数的函数。

Try this 尝试这个

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

$("#spnDownload").click(); $(“#spnDownload”)。click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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