How do I stop receiving: TypeError: $(…).attr(…) is undefined?

前端 未结 1 1435
Happy的楠姐
Happy的楠姐 2021-01-05 06:02

I\'m using jQuery 1.7.0, and I keep getting TypeError: $(...).attr(...) is undefined. Can someone please tell me what the problem is? My jQuery is below. Thank you.

1条回答
  •  无人及你
    2021-01-05 06:25

    http://api.jquery.com/attr/

    As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set.

    My guess is the attribute you are searching for isn't set on the element you're searching for it on. Due to this, .replace is failing because undefined doesn't have a replace method. You'll need to make sure it isn't undefined first.

    var current = $(this).attr('rel');
    current = current ? current.replace('aboutitemremove_', '') : '';
    

    do this for the id in the next area too.

    0 讨论(0)
提交回复
热议问题