Gaucho

Gaucho

fazedor de sites 🐘 • tentando ser dataísta 💪

GitHubTwitter

jQuery

cdn

<script src="https://unpkg.com/jquery"></script>

dark mode (detecção)

$(document).ready(function() {
    if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
        $('#modo').html('logo dark');
    }else{
        $('#modo').html('logo light');    
    }
});

getJSON

$.getJSON("arquivo.json",function(data){...});

Fonte: jQuery

parser de html (com hack)

function pegarFragmento(htmlStr,selStr){
    var $html = $('<div>'+htmlStr+'</div>');
    var conteudoDaDiv = $html.find(selStr).html();
    return conteudoDaDiv;   
}

plugin

(function($) {
    $.fn.mostrarAlerta = function(mensagem) {
        return this.each(function() {
            $(this).on('click', function() {
                alert(mensagem);
            });
        });
    };
})(jQuery);

$(document).ready(function() {
    $('#idDoBotao').mostrarAlerta('Esta é a mensagem de alerta.');
});

post

$.post("/user",{name:'Full Name'},function(data){
    alert( "resposta = " + data );
});

Fonte: jQuery

submeter form ao pressionar enter

$(document).ready(function() {
  const textarea=$("textarea");
  textarea.on("keydown",function(event) {
    if(event.keyCode===13){
      if(!event.shiftKey){
        event.preventDefault();
        $(this).closest("form").submit();
      }
    }
  });
});

verificar se o jquery está carregado

function executeAfterJQuery() {
    alert('jQuery está carregado. Execute seu código aqui.');
}
if(typeof jQuery=='undefined' && typeof Zepto=='undefined') {
    var script=document.createElement('script');
    script.src='https://cdnjs.cloudflare.com/ajax/libs/zepto/1.2.0/zepto.min.js';
    script.onload=function() {
        executeAfterJQuery();
    };
    document.head.appendChild(script);
} else {
    executeAfterJQuery();
}

Página inicial

GitHubTwitter