简体   繁体   English

另一个简单的jQuery问题

[英]Another Easy jQuery Question

I have the bellow code, I wish to create an IF statement so the loadScript function is only called for a specific HREF. 我有下面的代码,我想创建一个IF语句,以便只为特定的HREF调用loadScript函数。 But when I try and alert the value of the href it returns "Undefined"... 但是,当我尝试提醒href的值时,它将返回“未定义” ...

Many Thanks, J 非常感谢,J

$(document).ready(function()
        {
            $('.jNav').click(function()
            {
                $('#sandbox').load($(this).attr('href'),function() 
                {
                    alert("From here " + $(this).attr('href') + " to here.");
                    loadScript();
                });
                return false;
            });
        });

Your issue is just scoping. 您的问题只是范围界定。 In your load() callback, this refers to the element you're calling load() on, which happens to be $('#sandbox') , and thus: no href. 在您的load()回调中,这是指您要调用load()的元素,恰好是$('#sandbox') ,因此:no href。 Usually what I do is something like this: 通常我要做的是这样的:

$('.jNav').click(function()
{
  var self = this;
  $('#sandbox').load($(this).attr('href'),function() 
  {
    // Notice the use of self here, as we declared above
    alert("From here " + $(self).attr('href') + " to here.");
    loadScript();
  });
  return false;
});

Doing this ensures that you can still get to the thing that you clicked from inside the load() callback. 这样做可以确保您仍然可以从load()回调内部找到所单击的内容。

The problem is one of context: The call to $(this) in the alert refers to $('#sandbox') not $('.jNav') . 问题是上下文之一:警报中对$(this)的调用是指$('#sandbox')而不是$('.jNav') Simply define a variable for you first reference. 只需定义一个变量供您首次参考。

When you are inside the callback for $('#sandbox').load , this refers to $('#sandbox') , and not to $('.jNav') . 当您在$('#sandbox').load回调中时, this引用是$('#sandbox') ,而不是$('.jNav') If you want to alert the href, save that (or a reference to this ) in a variable. 如果要警告href,请将其(或this的引用)保存在变量中。

$('.jNav').click(function(){
  var that = this;
  $('#sandbox').load($(this).attr('href'),function(){
    alert($(that).attr('href'));
    //...
  });
}

OR 要么

$('.jNav').click(function(){
  var href = $(this).attr('href');
  $('#sandbox').load($(this).attr('href'),function(){
    alert(href);
    //...
  });
}

$(this) was the .jNav , but in your callback it's now the #sandbox . $(this).jNav ,但是在您的回调中它现在是#sandbox Pre-cache the variable at the point where it's presented to you, then use it wherever you like. 在将变量呈现给您的位置预缓存该变量,然后在任何需要的地方使用它。

Improving slightly on Groovetrain's answer, I'd write: 我在Groovetrain的答案上稍有改进,我写道:

$('.jNav').click(function(event) {
  var $self = $(this);
  var href  = $self.attr('href');

  $('#sandbox').load(href, function() {
    alert("From here " + href + " to here.");
    loadScript();
  });

  event.preventDefault(); // better than `return false`
});

Inside your innnermost function the context ( this ) is set to the #sandbox element. 在您最里面的函数中,上下文( this )被设置为#sandbox元素。 You'll want to get the href attribute from the .jNav element beforehand and store it in a variable. 您将需要事先从.jNav元素获取href属性并将其存储在变量中。

Example code 范例程式码

Take care of what "this" is in what part of your function. 请注意功能的哪个部分中的“ this”。 In the innermost function, you are calling it from $('#sandbox') ,which I suspect doesn't have a href attribute. 在最里面的函数中,您从$('#sandbox')调用它,我怀疑它没有href属性。 the best solution would probably be to pass the value of the href into the function or store it in a variable. 最好的解决方案可能是将href的值传递给函数或将其存储在变量中。

$(document).ready(function()
        {
            $('.jNav').click(function()
            {
                $('#sandbox').load($(this).attr('href'),function() 
                {
                    alert("From here " + $(this).parent().attr('href') + " to here.");
                    loadScript();
                });
                return false;
            });
        });

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

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