jQuery: noConflict

后端 未结 8 2096
無奈伤痛
無奈伤痛 2020-12-01 12:25

I just cannot work this out. My god it\'s making my brain hurt, so I turn to you, the good people of the internet.

I\'ve been staring at the documentation for the jQ

相关标签:
8条回答
  • 2020-12-01 13:11

    You should post an example that isn't working. I bet we'd clear it up immediately.

    I don't believe noConflict is a 'block' exactly. You use noConflict to tell jQuery that you want it to remove anything it declares from the global JavaScript namespace (because you want to load jQuery again and reuse those names).

    On a page with more than 1 instance of jQuery loaded, both instances should use noConflict. I don't know if it's possible to load a second instance of jQuery if there's already an instance loaded and that instance didn't call noConflict.

    @SLaks and @galambalazs: Sometimes you're writing a small amount of content that will be displayed within a larger page, such as a portal. The portal already uses a(n outdated) version of jQuery, but you need a newer version. Also you don't control the larger portal page, just the content you're writing that will be displayed within it.

    This scenario accurately describes real work I do commonly.

    0 讨论(0)
  • 2020-12-01 13:17

    I would agree with other posts that suggest trying to upgrade to 1.4.2 across the board... That said, you clearly have a good reason for attempting what you are trying to do. To my knowledge there is no easy way to deal with your situation (as I too have tried something similar).

    "noConflict" simply releases the global "$" variable so that other libraries/scripts may safely use it. Given that you are trying to use two versions of jQuery "noConflict" isn't really helpful here...calling it for both versions of jQuery doesn't change the fact that both versions still need to reference the global "jQuery" object (of which there can only be one...).

    The issue (as you clearly already know) is that loading the 2nd jQuery version will "clobber" the original jQuery object (and all the "customizations" made to it by plug-ins).

    The only reliable (albeit incredibly inefficient) solution I could come up with is:

    • Load the existing (old) jQuery version and plugins (assuming you can't avoid this)
    • Load the new jQuery version
    • RE-load and existing plugins and any new plugin you want to use
    • Test extensively
    0 讨论(0)
提交回复
热议问题