Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 另一个简单的jQuery问题_Javascript_Jquery - Fatal编程技术网

Javascript 另一个简单的jQuery问题

Javascript 另一个简单的jQuery问题,javascript,jquery,Javascript,Jquery,我有下面的代码,我希望创建一个IF语句,以便只为特定的HREF调用loadScript函数。但当我尝试警告href的值时,它返回“Undefined” 非常感谢,, J 在InnerMOST函数中,上下文(this)设置为#sandbox元素。您需要事先从.jNav元素获取href属性,并将其存储在变量中 $('.jNav').click(function(){ var that = this; $('#sandbox').load($(this).attr('href'),functi

我有下面的代码,我希望创建一个IF语句,以便只为特定的HREF调用loadScript函数。但当我尝试警告href的值时,它返回“Undefined”

非常感谢,, J


在InnerMOST函数中,上下文(
this
)设置为#sandbox元素。您需要事先从.jNav元素获取href属性,并将其存储在变量中

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

您的问题只是范围界定。在load()回调中,这是指调用load()的元素,它恰好是
$('#sandbox')
,因此:no href。通常我会这样做:

$('.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;
});

这样做可以确保您仍然可以从load()回调内部访问单击的内容。

问题在于上下文:警报中对
$(this)
的调用指的是
$(“#沙盒”)
而不是
$('.jNav')
。只需为第一次引用定义一个变量。

注意函数的哪一部分中的“this”是什么。在最里面的函数中,您从
$(“#sandbox”)
调用它,我怀疑它没有href属性。最好的解决方案可能是将href的值传递到函数中或将其存储在变量中。

当您在
$('.\sandbox')的回调中时。加载
指的是
$('.\sandbox')
,而不是
$('.jNav')
。如果要提醒href,请将其(或对
this
的引用)保存在变量中

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

$(this)
.jNav
,但在您的回调中,它现在是
沙箱。在变量呈现给您的位置预缓存变量,然后在您喜欢的任何地方使用它

对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`
});

如果这么简单的话,我想你已经知道怎么做了!哇!2分钟内有这么多答案。这一定很容易,正如OP所承诺的:)。当我在通知栏中看到“发布了4个新答案”时,我停止了写我的答案。
.jNav
可能不是
#sandbox
的父项。你需要将
事件
传递到单击函数中。你只需使用
var self=this.href@无菌真实数据。但是,一般来说,最好像这样确定调用对象的范围,以防您需要它的目的不仅仅是它的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;
            });
        });
$('.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`
});