点击任意页面打开链接

时间:2014-10-10 16:33:06

标签: javascript

我需要一种方法在任意鼠标左键单击页面上打开特定链接(例如www.google.it)

例如,如果有一个名为“home”的按钮并且用户点击它,它将打开两页home.htm和www.google.it

有办法吗?

我正在尝试使用简单的javascript代码,但它不适用于超大化:)

http://inogroup.it/new/313/slideshow/fade.html

2 个答案:

答案 0 :(得分:0)

只需使用window.open即可。您可以在此处阅读更多内容:http://www.w3schools.com/jsref/met_win_open.asp

例如,使用jQuery的代码就是这样(假设链接的id为“home”):

$("#home").on("click", function() {
    window.open("http://www.google.it");
});

另一个例子没有使用jQuery,只是简单的旧JavaScript:

document.getElementById("home").onclick = function() { window.open("http://www.google.it"); }

您的链接将在当前标签上打开,其他网址(google.it)将在新标签页上打开。

答案 1 :(得分:0)

基于Alvaro的回答纯粹的java脚本解决方案

document.addEventListener('click', function(event) {
    if (/* your disabled check here */) {
      // Kill the event
      event.preventDefault();
      event.stopPropagation();
    }

    // Doing nothing in this method lets the event proceed as normal
  },
  true  // Enable event capturing!
);

这个答案建立在这个先前回答的问题上。 JavaScript - Hook in some check on all 'click' events

相关问题