1

我之前问过这个问题,但一些专家告诉我补充

<meta http-equiv="refresh" content="2;url=http://www.example.com/" />

这可以重新加载给定的链接,但我想知道如何在 id 的帮助下单击元素(锚点)。是否有任何代码在执行时会单击 id='dp99',并且我希望在访问页面时执行此 javascript。这是HTML

<a id='d99' href='http://someline.com'>This is a link</a>

如果有人可以帮助我,我将不胜感激!谢谢你。

4

2 回答 2

3

使用 jQuery:

$(document).ready(function() { $('#dp99').click(); });

没有 jQuery:

document.addEventListener('DOMContentLoaded', function () {
  document.getElementById('dp99').click();
});
于 2012-05-31T01:46:24.800 回答
0

如在http://marcgrabanski.com/articles/simulating-mouse-click-events-in-javascript上所见

function mouseEvent(type, sx, sy, cx, cy) {

  var evt;

  var e = {

    bubbles: true, cancelable: (type != "mousemove"), view: window, detail: 0,

    screenX: sx, screenY: sy, clientX: cx, clientY: cy,

    ctrlKey: false, altKey: false, shiftKey: false, metaKey: false,

    button: 0, relatedTarget: undefined

  };



  if (typeof( document.createEvent ) == "function") {

    evt = document.createEvent("MouseEvents");

    evt.initMouseEvent(type, e.bubbles, e.cancelable, e.view, e.detail,

    e.screenX, e.screenY, e.clientX, e.clientY,

    e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,

    e.button, document.body.parentNode);

  } else if (document.createEventObject) {

    evt = document.createEventObject();

    for (prop in e) {

      evt[prop] = e[prop];

    }

    evt.button = { 0:1, 1:4, 2:2 }[evt.button] || evt.button;

  }

  return evt;

}



function dispatchEvent (el, evt) {

  if (el.dispatchEvent) {

    el.dispatchEvent(evt);

  } else if (el.fireEvent) {

    el.fireEvent(‘on’ + type, evt);

  }

  return evt;

}
于 2012-05-31T02:07:05.597 回答