2 JS functions with same name conflict

后端 未结 5 1064
野性不改
野性不改 2021-02-15 15:17

Short

Using 2 libraries at same page: jQuery UI and Twitter Bootstrap:

  • jQuery UI very important for me because nearly all UI things built based on it
相关标签:
5条回答
  • 2021-02-15 15:28

    To fix the collision between Bootstrap and jQuery UI functions, rename one of them:

    <script type="text/javascript" src="bootstrap.min.js"></script>
    <script type="text/javascript">
        $.fn.bsbutton = $.fn.button;
    </script>
    <script type="text/javascript" src="jquery-ui.min.js"></script>
    

    And then you can call each function at will:

    <script type="text/javascript">
        $(".button-one").button()   // jQueryUI button
        $(".button-two").bsbutton()  // Bootstrap button
    </script>
    

    You can use this technique for any function you need.

    Remember the order include A -> rename A -> include B.

    0 讨论(0)
  • 2021-02-15 15:28

    All other answers fixing conflict between jQuery UI button and bootstrap button but you can't use bootstrap data-api for renamed button() function. Only manually using new-named bootstrapBtn() function.

    I find a solution:

    • Find and replace all occurences of regex (\$[.\w]+\.)button to \1btn in bootstrap.js file.
    • Or use this sed script:

      #!/bin/bash
      # Change bootstrap3 $.fn.button() function to $.fn.btn() for no conflict with jQuery UI
      sed "s/\(\$[[:alnum:]._]\+\.\)button/\1btn/g" bootstrap/js/bootstrap.js > bootstrap/js/bootstrap.btn.js
      sed "s/\(fn\.\)button/\1btn/g" bootstrap/js/bootstrap.min.js | sed "s/button\((\)/btn\1/g" > bootstrap/js/bootstrap.btn.min.js
      

    After that you can use bootstrap data-api for button binding and also manually use btn() function in place of button() for Bootstrap while still use jQuery UI button() function.

    0 讨论(0)
  • 2021-02-15 15:29

    I suggest to do custom jQueryUI, or Bootstrap build - both offer this possibility on websites.

    I use custom build of jQueryUI with core, effects, interactions, but without widgets, which often causes conflicts.

    0 讨论(0)
  • 2021-02-15 15:44

    The current version of bootstrap (v2.2.2) now has a noConflict option. Just insert this some place after your bootstrap and jquery script tags but before any usage of the button() function.

    <script type='text/javascript'>
        $.fn.bootstrapBtn = $.fn.button.noConflict();
    </script>
    

    Alternatively, you can use $(document).ready(handler), but you still have to make sure substitution occurs before button() is called.

    Once that line of code is executed, button() will be JqueryUI's button, and bootstrapBtn() will be Bootstrap's version.

    0 讨论(0)
  • 2021-02-15 15:47

    If you are only using the dropdown plugin of Twitter Bootstrap, you don't need the .button() plugin.

    Go to the customize bootstrap page and unselect all jQuery plugins, then choose only the dropdown one. You could also unselect some of the CSS if you want.


    If you need both implementations (Twitter Bootstrap and jQuery UI), you could rename all .button to something like .bsButton in the bootstrap-button.js file (or find this section in a non-minified version) - not tested.

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