Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.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 target.attr不是函数,长度未定义jquery onclick_Javascript_Jquery - Fatal编程技术网

Javascript target.attr不是函数,长度未定义jquery onclick

Javascript target.attr不是函数,长度未定义jquery onclick,javascript,jquery,Javascript,Jquery,我有两个不同类的点击事件,它们有一部分代码对它们来说都是相同的,所以我尝试创建一个函数,其中包含相同的代码部分,然后在onclick中调用该函数,但我一直得到一个错误 这是正常工作的代码: $(list).on('click', function() { $(arrow).removeClass("mobile-select__arrow--active") $(listItem).removeClass(listItem_expanded); $(

我有两个不同类的点击事件,它们有一部分代码对它们来说都是相同的,所以我尝试创建一个函数,其中包含相同的代码部分,然后在onclick中调用该函数,但我一直得到一个错误

这是正常工作的代码:

$(list).on('click', function() {

    $(arrow).removeClass("mobile-select__arrow--active")
    $(listItem).removeClass(listItem_expanded);
    $(list).addClass("mobile-select--sticky");
    var windowHeight = $(window).height();
    var documentHeight = $(document).height();
    var target = $(this.getAttribute('href'));
    if (target.length) {
      //prevent default bhebahvior
      event.preventDefault();
      var topDistance = target.offset().top;
      var bottomDistance = documentHeight - topDistance;
      if (windowHeight > bottomDistance) {
        $(container).css('padding-bottom', topDistance + 'px');
      };
      var stickyTopHeight = $(list).outerHeight();
      $('html, body').stop().animate({
        scrollTop: target.offset().top - stickyTopHeight - 150
      }, 1000);
  
        if($(window).width() <= 769) {
            $('html, body').stop().animate({
                scrollTop: target.offset().top - stickyTopHeight - 100
            }, 1000);
        }
    }
    if($(window).width() <= 769) {
        if (!$(this).hasClass(list_expanded)) {
            $(this).addClass(list_expanded);
            $(arrow).addClass("mobile-select__arrow--active")
            $(listItem).addClass(listItem_expanded);

        } else {
            $(this).removeClass(list_expanded);
            // $(this).slideUp();
            $(arrow).removeClass("mobile-select__arrow--active")
            $(listItem).removeClass(listItem_expanded);
        }
    }
    if ($(window).width() > 769) {
        $(this).addClass(list_expanded);
        $(arrow).addClass("mobile-select__arrow--active")
        $(listItem).addClass(listItem_expanded);
    }
    
});

$(link).on('click', function() {

    $(arrow).removeClass("mobile-select__arrow--active")
    $(listItem).removeClass(listItem_expanded);
    $(list).addClass("mobile-select--sticky");
    var windowHeight = $(window).height(); 
    var documentHeight = $(document).height();
    var target = $(this.getAttribute('href'));
    if (target.length) {
        //prevent default bhebahvior
        event.preventDefault();
        var topDistance = target.offset().top;
        var bottomDistance = documentHeight - topDistance;
        if (windowHeight > bottomDistance) {
            $(container).css('padding-bottom', topDistance + 'px');
        }
        var stickyTopHeight = $(list).outerHeight();
        $('html, body').stop().animate({
            scrollTop: target.offset().top - stickyTopHeight - 150
        }, 1000);
        if($(window).width() <= 769) {
            $('html, body').stop().animate({
                scrollTop: target.offset().top - stickyTopHeight - 100
            }, 1000);
        }
    }
});
$(列表)。在('click',function()上{
$(箭头).removeClass(“移动选择箭头--活动”)
$(listItem).removeClass(listItem_展开);
$(list.addClass(“移动选择--粘性”);
var windowHeight=$(window.height();
var documentHeight=$(document).height();
var target=$(this.getAttribute('href');
if(目标长度){
//防止默认bhebahvior
event.preventDefault();
var topDistance=target.offset().top;
var bottomDistance=文档高度-顶部距离;
如果(窗口高度>底部距离){
$(container.css('padding-bottom',topDistance+'px');
};
var stickyTopHeight=$(list.outerHeight();
$('html,body').stop().animate({
scrollTop:target.offset().top-stickyTopHeight-150
}, 1000);
如果($(窗口).width()底部距离){
$(container.css('padding-bottom',topDistance+'px');
}
var stickyTopHeight=$(list.outerHeight();
$('html,body').stop().animate({
scrollTop:target.offset().top-stickyTopHeight-150
}, 1000);

如果($(window).width()这可能是失败的原因:

$(link).on('click', doSomething);
doSomething
函数期望它的一个参数是DOM元素的jQuery对象,但是
click
事件传递的是事件对象,而不是调用事件的元素。您可以像在其他
click
处理程序中一样,将元素作为jQuery对象传递:

$(link).on('click', function () { doSomething($(this)); });

$(link).on('click',doSomething);
作为事件处理程序将把
事件传递给方法,而不是直接传递给目标。
函数doSomething(event){$(event.target).attr(…);}
根据
链接的内容,使用可以传入的第二个代码段,而不是使用
,作为替代方法。或者只需更改
doSomething
即可将目标从事件中移除并避免此额外的包装方法
$(link).on('click', doSomething);
$(link).on('click', function () { doSomething($(this)); });