Javascript/jQuery:以编程方式关注链接

Dan*_*Dan 16 html javascript hyperlink

在Javascript代码中,我想以编程方式使浏览器关注我页面上的链接.简单案例:

<a id="foo" href="mailto:somebody@example.com">something</a>
Run Code Online (Sandbox Code Playgroud)
function goToBar() {
   $('#foo').trigger('follow');
}
Run Code Online (Sandbox Code Playgroud)

这是假设,因为它实际上不起作用.不,触发click不会这样做.

我知道window.location并且window.open这些与本地链接跟踪有所不同,在某些方面对我来说很重要:a)存在<base />元素,b)mailtoURL 的情况.后者尤其重要.至少在Firefox中,调用window.location.href = "mailto:somebody@example.com"导致窗口的unload处理程序触发,而mailto根据我的判断,只是单击链接.

我正在寻找一种方法来触发浏览器的默认链接处理,来自Javascript代码.

这种机制存在吗?工具包特定的答案也欢迎(特别是对于Gecko).

Tor*_*ter 23

据我所知,window.location完全符合您的要求,触发浏览器的默认链接点击行为.

某些浏览器在触发任何事件或更改实际href之前会注意到该协议.

window.location = "mailto:somebody@example.com";
Run Code Online (Sandbox Code Playgroud)

尝试下面提到的小提琴演示我得到以下结果:

  • 铬:onbeforeunload使用按钮和链接触发
  • Firefox onbeforeunload仅针对按钮触发
  • Safari:永远不会开火 onbeforeunload
  • Opera:与Safari相同

因此,防止unload事件被触发的好方法是返回false beforeunload.


Jua*_*des 6

方法 1单击方法

HTMLElements 有一个方法click() https://developer.mozilla.org/en/DOM/element.click

function goToBar() {
   document.getElementById('foo').click();
}
Run Code Online (Sandbox Code Playgroud)

方法 2触发合成事件

我想知道为什么 saluc 删除了他的回答。该解决方案是我过去使用过的(当单击仅用于 IE 时)。也就是说,触发合成浏览器事件(不是像 jQuery 那样的虚假事件click())。让我使用这个想法发布一个解决方案......

演示:http : //jsfiddle.net/eyS6x/3/

/**
 * Fire an event handler to the specified node. Event handlers can detect that the event was fired programatically
 * by testing for a 'synthetic=true' property on the event object
 * @param {HTMLNode} node The node to fire the event handler on.
 * @param {String} eventName The name of the event without the "on" (e.g., "focus")
 */
function fireEvent(node, eventName) {
  // Make sure we use the ownerDocument from the provided node to avoid cross-window problems
  var doc;
  if (node.ownerDocument) {
    doc = node.ownerDocument;
  } else if (node.nodeType == 9 /** DOCUMENT_NODE */){
    // the node may be the document itself
    doc = node;
  } else {
    throw new Error("Invalid node passed to fireEvent: " + +node.tagName + "#" + node.id);
  }

  if (node.fireEvent) {
    // IE-style
    var event = doc.createEventObject();
    event.synthetic = true; // allow detection of synthetic events
    node.fireEvent("on" + eventName, event);
  } else if (node.dispatchEvent) {
    // Gecko-style approach is much more difficult.
    var eventClass = "";

    // Different events have different event classes.
    // If this switch statement can't map an eventName to an eventClass,
    // the event firing is going to fail.
    switch (eventName) {
      case "click":
      case "mousedown":
      case "mouseup":
        eventClass = "MouseEvents";
        break;

      case "focus":
      case "change":
      case "blur":
      case "select":
        eventClass = "HTMLEvents";
        break;

      default:
        throw "JSUtil.fireEvent: Couldn't find an event class for event '" + eventName + "'.";
        break;
    }
    var event = doc.createEvent(eventClass);
    var bubbles = eventName == "change" ? false : true;  
    event.initEvent(eventName, bubbles, true); // All events created as bubbling and cancelable.

    event.synthetic = true; // allow detection of synthetic events
    node.dispatchEvent(event);
  }
};

document.getElementById('button').onclick = function() {
   fireEvent( document.getElementById('link'), 'click');
}
Run Code Online (Sandbox Code Playgroud)