最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript TypeError: $(...).parent is not a function - Stack Overflow

programmeradmin0浏览0评论

I am having this error when I click on the following code:

onclick="tester.removeit(this);"

I get Error:

TypeError: $(...).parent is not a function

Here is the function:

removeit: function(ele) {

    $(ele).parent('div').fadeOut();
    console.log(this);

},

How can I fix this?

I am having this error when I click on the following code:

onclick="tester.removeit(this);"

I get Error:

TypeError: $(...).parent is not a function

Here is the function:

removeit: function(ele) {

    $(ele).parent('div').fadeOut();
    console.log(this);

},

How can I fix this?

Share Improve this question asked May 4, 2016 at 10:35 Satch3000Satch3000 49.4k90 gold badges224 silver badges349 bronze badges 5
  • have you given the reference to jQuery? If you are loading jQuery from CDN, make sure you are online – Irshu Commented May 4, 2016 at 10:38
  • 1 before the .parent section, add a console.log(ele); I think ele is not a jQuery object. – vaso123 Commented May 4, 2016 at 10:39
  • 3 Working fine: jsfiddle/rayon_1990/ge1Lwtr6 And if the question is about this, "The value of this depends on how function is called!" – Rayon Commented May 4, 2016 at 10:39
  • 1 Sounds like you're including some other library (like PrototypeJS or MooTools), not jQuery. (If you weren't including anything, the error would be plaining about $, not parent.) – T.J. Crowder Commented May 4, 2016 at 10:43
  • Spot on! It was a library conflict with jQuery. Thanks – Satch3000 Commented May 4, 2016 at 10:45
Add a ment  | 

1 Answer 1

Reset to default 3

Sounds like a library conflict, where you're including PrototypeJS or MooTools after including jQuery.

When you do that, only one library can use $ as its main identifier. You can tell jQuery to "release" $ via noConflict:

<script src="jquery.js"></script>
<script>jQuery.noConflict();</script>
<script src="prototypejs.js"></script>

Then in code where you want to use jQuery, use jQuery rather than $:

// ...
removeit: function(ele) {

    jQuery(ele).parent('div').fadeOut();
    console.log(this);

},
// ...

Or wrap all of your code using jQuery in an IIFE that accepts $ as an arg:

(function($) {
    // ...
    removeit: function(ele) {

        $(ele).parent('div').fadeOut();
        console.log(this);

    },
    // ...
})(jQuery);
发布评论

评论列表(0)

  1. 暂无评论