简体   繁体   English

Javascript需要使用<a href>ID链接</a>传递变量

[英]Javascript need to pass variable using <a href> link with id

I am using the code below for loading an external page into a lightbox. 我正在使用下面的代码将外部页面加载到灯箱中。 How could I pass from the href tag, a variable named 'page' to the javascript (line 1) containing the value of a page link = 'country.php'. 我如何从href标记(一个名为“ page”的变量)传递到包含页面链接=“ country.php”值的javascript(第1行)。 I am wanting to use this script call several times without having to have additional javascript lines for each link. 我想多次使用此脚本调用,而不必为每个链接添加其他javascript行。

<script>
$("a#selector").live("click", function(){$(".overlayInner").load("/country.php"); })
$("a#selector").live("click", function(){$(".overlayOuter").fadeIn(300); })
$("a#selector").live("click", function(){$(".overlayInner").fadeIn(500); })
$("a#selector").live("click", function(){$(".closepop").fadeIn(500); })
</script>


<a id="selector" href="#"><img src="/images/somegraphic.gif"></a>

Any help is much appreciated. 任何帮助深表感谢。

Couple points: 几点:

  1. The jQuery attr() method can be used to get any attribute, including the href , on any element. jQuery attr()方法可用于获取任何元素上的任何属性,包括href

  2. Use the on() method with jQuery 1.7+ as live() is deprecated. 由于不建议使用live()请在jQuery 1.7+ on()使用on()方法。

  3. If you will have multiple <a> tags, you will need to refer to a class name instead of an ID . 如果将有多个<a>标记,则需要引用一个class名而不是ID So you would use a "." 因此,您将使用“。” instead of a "#". 代替 ”#”。 In your case, it would be .selector instead of #selector . 在您的情况下,它将是.selector而不是#selector

  4. To reduce the number of statements inside the click function, you can combine the two fadeIn() functions that have the same time interval, 500 . 为了减少click函数中的语句数量,可以将两个时间间隔为500 fadeIn()函数组合在一起。 Note below that you would use a "," to separate elements, in this case, .overlayInner and .closepop . 在下面请注意,您将使用“,”分隔元素,在这种情况下,是.overlayInner.closepop

  5. Since you are in a lightbox, the assumption is you will not want to migrate away to another page when you click the link. 由于您在灯箱中,因此假设您不希望在单击链接时迁移到另一个页面。 For this reason, you will need to use either e.preventDefault or return false; 因此,您将需要使用e.preventDefaultreturn false; as the last statement inside the click function. 作为click函数中的最后一条语句。 The e , would be the eventObject passed into the anonymous 'click', 'a.selector', function(e) . e是传递给匿名 'click', 'a.selector', function(e)eventObject

Taken together, this is how your javascript could look. 综上所述,这就是您的JavaScript外观。

var href=$("a.selector").attr('href', 'country.php');

$(document.body).on("click", "a.selector", function(){
    $(".overlayInner").load("'+href+'");
    $(".overlayOuter").fadeIn(300); 
    $(".overlayInner, .closepop").fadeIn(500); 
    return false;
});

Try this: 尝试这个:

$("a.selector").live("click", function() {
    var url = $(this).attr('href'); 
    $(".overlayInner").load(url);
    return false;
});

<a class="selector" href="/country.php">Country</a>
<a class="selector" href="/pigs.php">Pigs</a>

If you want to get the inner text that says "Country", or an image you can do: 如果要获取显示“国家”的内部文本或图像,可以执行以下操作:

$("a.selector").live("click", function() {
    var url = $(this).attr('href'); 
    var img = $(this).html(); // But what do you want to do with it?
    $(".overlayInner").load(url);
    return false;
});

<a class="selector" href="/country.php"><img src="img.jpg" /></a>

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

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