简体   繁体   English

用javascript链接做通用脚本

[英]do general script for links with javascript

I have a script like: 我有一个像这样的脚本:

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

and I am using it as 我用它作为

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

How can I make only one script and pass as variable the url I want to open? 如何只制作一个脚本并将要打开的URL作为变量传递?

You can use this.href to get the specific link's href to navigate to open in window but make sure to stop the link's default behavior with .preventDefault() : 您可以使用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>

Try like this 这样尝试

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

You can use .prop also like 您也可以使用.prop

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

You can directly use this HTML code.May be it will helps you 您可以直接使用此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