无法读取未定义的属性"attr" [英] Cannot read property 'attr' of undefined

查看:236
本文介绍了无法读取未定义的属性"attr"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的代码出现此错误?

Why am I getting this error on my code?

无法读取未定义的属性'attr'

Cannot read property 'attr' of undefined`

$(document).ready(function() { 
    var currentPage = jQuery.url.attr("path");
    $(':input').blur(function () {
        if ($(this).val().length > 0) {
            pageTracker._trackEvent("Form: " + currentPage, "input_exit", $(this).attr('name'));
        } 
    });
});

我的小提琴: https://jsfiddle.net/4ocdcqrf/

推荐答案

如果您在 jQuery.url 上进行控制台登录,您将看到这是一个未定义的值.

If you do a console log on jQuery.url, you will see this is has a value of undefined.

您将需要使用 location.pathname 而不是 jQuery.url .

此处以进行正确理解

这篇关于无法读取未定义的属性"attr"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆