Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JQuery从链接打开新窗口_Javascript_Jquery_Html_Window - Fatal编程技术网

Javascript 使用JQuery从链接打开新窗口

Javascript 使用JQuery从链接打开新窗口,javascript,jquery,html,window,Javascript,Jquery,Html,Window,我已经找到了一些在这个论坛中应该有效的方法,但对我来说不起作用。 我需要按下一个链接,然后在一个新窗口中打开一个新窗口 我的HTML代码是: <!DOCTYPE html> <html lang="sv-se"> <head> <meta charset="UTF-8"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.m

我已经找到了一些在这个论坛中应该有效的方法,但对我来说不起作用。 我需要按下一个链接,然后在一个新窗口中打开一个新窗口

我的HTML代码是:

  <!DOCTYPE html>
  <html lang="sv-se">
  <head>
    <meta charset="UTF-8">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="Navigering.js"></script>
    <title>Navigering</title>
</head>
<html>
<body>
<h1>Navigering</h1>
<a href="https://www.google.se/" alt="google">Link toGoogle</a>
</body>
代码应为:

$(document).ready(function(){
  $("a").attr('target','_blank');
}
编辑:

如果此解决方案不适用于您,您是否可能在以后动态加载链接?因为在这种情况下,该脚本不会检测到新添加的链接,也不会对其进行修改。在这种情况下,您可以使用以下方法:

$(document).ready(function(){
    $('document').on('click','a', function() {
      window.open( $(this).attr('href') );
      return false;
    });
});

为此,请尝试以下代码:

<!DOCTYPE html>
<html lang="sv-se">
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="Navigering.js"></script>
<title>Navigering</title>
</head>
<body>
<h1>Navigering</h1>
<a href="https://www.google.se/" onclick="window.open(this.href, 'newwindow', 'width=300, height=250'); return false;" alt="google">Link toGoogle</a>
</body>
</html>

Here, If we click on "LinktoGoogle" link then it will open link in new window (also we can set height and width of new window) and there is no need to add any jquery code for this.

航海
航海
在这里,如果我们点击“LinkToLogle”链接,它将在新窗口中打开链接(我们也可以设置新窗口的高度和宽度),并且不需要为此添加任何jquery代码。

您编写了一个函数,但它是从哪里调用的?另外,您可以将这个函数绑定到ready函数中。为什么不在
html
中添加相同的属性呢?这就是问题所在,我是JQuery新手,我很难理解该如何称呼它……问题很模糊,有很多可能的原因导致它不起作用:1-
Navigering.js
可能返回404。2-代码有一个语法错误,如所述。3-在DOM完全准备好所有图像和资源后,您不需要在DOM中加载窗口readyWindow load。谢谢,但它不会工作。。我见过很多这种解决方案,但我不认为它对我有用。。
<!DOCTYPE html>
<html lang="sv-se">
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="Navigering.js"></script>
<title>Navigering</title>
</head>
<body>
<h1>Navigering</h1>
<a href="https://www.google.se/" onclick="window.open(this.href, 'newwindow', 'width=300, height=250'); return false;" alt="google">Link toGoogle</a>
</body>
</html>

Here, If we click on "LinktoGoogle" link then it will open link in new window (also we can set height and width of new window) and there is no need to add any jquery code for this.