繁体   English   中英

用javascript链接做通用脚本

[英]do general script for links with javascript

我有一个像这样的脚本:

<script  type="text/javascript">
     $("a.link").on("click",function(){
         window.open('http://site.com/fir.php','_self',false);
     });
</script>

我用它作为

<a  href="fir.php" class="link">
fir
</a>

如何只制作一个脚本并将要打开的URL作为变量传递?

您可以使用this.href来获取特定链接的href进行导航以在窗口中打开,但请确保使用.preventDefault()停止链接的默认行为:

<script  type="text/javascript">
    $("a.link").on("click",function(e){
       e.preventDefault();// or return false; 
       window.open('http://site.com/'+this.href, '_self', false);
    });
 </script>

这样尝试

<script  type="text/javascript">
 $("a.link").on("click",function(){
     window.open('http://site.com/'+$(this).attr('href'),'_self',false);
 });
 </script>

您也可以使用.prop

window.open('http://site.com/'+$(this).prop('href'),'_self',false);

您可以直接使用此HTML代码。也许对您有帮助

 $(function(){
  $("a.link").on("click",function(e){
    window.open(this.href, '_self', false);
  });
 });

暂无
暂无

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

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