Conflict between a Jquery call and a js file

0

I have a problem in a function using Jquery and a file that I call inside my html that are conflicting, it is only working one, I tried using jQuery.noConflict () but apparently it did not work, I do not know how to use it correctly follows the script and call that they are conflicting:

<script>
var $JQuery = jQuery.noConflict()
$jQuery(document).ready(function($) { 
   $(".page-scroll").click(function(event){        
    event.preventDefault();
   $('html,body').animate({scrollTop:$(this.hash).offset().top}, 1000);
    });
});
</script>  
<script src="/js/demo.js"></script>
    
asked by anonymous 05.05.2017 / 13:27

1 answer

0

The argument passed to the callback of the ready method refers to the selection, in this case document . That is, you are getting the variable $ as a reference to document and not jQuery .

Probably what you want to do is something like the examples below:

// utilizar a variável que você criou $jQuery
var $JQuery = jQuery.noConflict()
$JQuery(document).ready(function() {
    $JQuery(".page-scroll").click(function() { /* ... */ });
});

// ou remover a variável $ do escopo global e utilizar apenas como jQuery
jQuery.noConflict()
jQuery(document).ready(function() {
    jQuery(".page-scroll").click(function() { /* ... */ });
});

// ou OUTRA biblioteca semelhante ao jQuery que use o $
jQuery.noConflict()
jQuery(document).ready(function() {
    $(".page-scroll").click(function() { /* ... */ });
});

// ou utilizar o jQuery como $ apenas neste arquivo e não globalmente
jQuery.noConflict()
(function($) {
    $(document).ready(function() {
        $(".page-scroll").click(function() { /* ... */ });
    });
}(jQuery));
    
05.05.2017 / 13:54