简体   繁体   English

如何在单个变量名称中使用多个url值

[英]How can i take multiple url value in single variable name

In below I want to pass a variable value from one page to other using url redirection. 在下面,我想使用url重定向将变量值从一页传递到另一页。 But in my variable I have multiple url, so that when I click on particular url only that url value will store and redirected. 但是在我的变量中,我有多个网址,因此,当我单击特定的网址时,只会存储并重定向该网址值。

 <script src="jquery-1.11.3.js"></script> <script> $(document).ready(function(){ $('a').click(function(e){ e.preventDefault(); var clk=$('#fkt').attr('href'); var clk=$('#snd').attr('href'); var clk=$('#amz').attr('href'); //alert(clk); window.location='http://www.shopeeon.com?ver='+clk; }); }); </script> <a id='fkt' href="http://www.google.com">Google</a><br/> <a id='snd' href="http://www.yahoo.com">Yahoo</a><br/> <a id='amz' href="http://www.rediff.com">Rediff</a><br/> 

In my case it will only take last URL link and stored in clk variable. 就我而言,它将仅使用最后一个URL链接并存储在clk变量中。

To get the href of the current clicked <a> tag use this : 要获取当前单击的<a>标记的href ,请使用this代码:

$('a').click(function(e){
    e.preventDefault();
    var clk = $(this).attr('href');
    window.location='http://www.shopeeon.com?ver='+clk;
});

The reason why it doesn't work is, you are redeclaring the same string again and again, and it wipes out old content. 它不起作用的原因是,您一次又一次地重新声明了相同的字符串,并且它清除了旧内容。 You may use this: 您可以使用此:

var clk = $('#fkt').attr('href');
clk += $('#snd').attr('href');
clk += $('#amz').attr('href');

This concatenates the string. 这将字符串连接起来。

Can you use an array? 可以使用数组吗?

 var clk = []; clk.push($('#fkt').attr('href')); clk.push($('#snd').attr('href')); clk.push($('#amz').attr('href')); console.log(clk.join(",")); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <a id='fkt' href="http://www.google.com">Google</a><br/> <a id='snd' href="http://www.yahoo.com">Yahoo</a><br/> <a id='amz' href="http://www.rediff.com">Rediff</a><br/> 

You should use the contextual this . 您应该使用上下文this But ultimately, what you are looking for is: 但最终,您要寻找的是:

$('a').click(function (e) {
  e.preventDefault();
  var clk = $(this).attr('href');
  location.href = 'http://www.shopeeon.com?ver='+clk;
});

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

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