Skip to content

Instantly share code, notes, and snippets.

@CastenettoA
Forked from liamcurry/gist:2597326
Last active April 28, 2017 07:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save CastenettoA/5787297 to your computer and use it in GitHub Desktop.
Save CastenettoA/5787297 to your computer and use it in GitHub Desktop.
Muoversi da jQuery a Javascript non è difficile, anzi, può essere divertente e sicuramente stimolante. Impara la vera potenza di Javascript seguendo questo breve tutorial. Senza jQuery, Javascript, risulterà molto più performante, veloce e se vuoi flessibile...

Muoversi da jQuery a javascript

Usare Javascript invece che jQuery nei tuoi progetti può essere una mossa molto intelligente, non solo imparerai più a fondo le dinamiche del linguaggio e diventarai naturalmente più esperto, ma incrementerai notevolmente le performance del tuo sito.

Se lavori in grossi progetti e applicazioni che richiedono tante linee di codice lavorare in puro Javascript è un po frustrante, in questo caso un mix di JQuery e Javascript è necessario.

  1. [Eventi] (#eventi)
  2. [Selettori] (#selettori)
  3. [Modifica Attributi] (#modifica)
  4. [Aggiungere o togliere una classe] (#addremove)
  5. [Manipolazione del DOM] (#DOM)
  6. [Transversing, muoversi nel DOM] (#transversing)
  7. [AJAX] (#ajax)
  8. [JSONP] (#jsonp)
// jQuery
$(document).ready(function() {
  // code
})

// javascript
document.addEventListener('DOMContentLoaded', function() {
  // code
})
// jQuery
$('a').click(function() {
  // code…
})

// javascript
[].forEach.call(document.querySelectorAll('a'), function(el) {
  el.addEventListener('click', function() {
    // code…
  })
})

[torna all'indice ↑] (#TOC)

// jQuery
var divs = $('div')

// javascript
var divs = document.querySelectorAll('div')
// jQuery
var newDiv = $('<div/>')

// javascript
var newDiv = document.createElement('div')

[torna all'indice ↑] (#TOC)

// jQuery
$('img').filter(':first').attr('alt', 'My image')

// javascript
document.querySelector('img').setAttribute('alt', 'My image')

[torna all'indice ↑] (#TOC)

// jQuery
newDiv.addClass('foo')

// javascript
newDiv.classList.add('foo')
// jQuery
newDiv.toggleClass('foo')

// javascript
newDiv.classList.toggle('foo')

[torna all'indice ↑] (#TOC)

// jQuery
$('body').append($('<p/>'))

// javascript
document.body.appendChild(document.createElement('p'))
// jQuery
var clonedElement = $('#about').clone()

// javascript
var clonedElement = document.getElementById('about').cloneNode(true)
// jQuery
$('#wrap').empty()

// javascript
var wrap = document.getElementById('wrap')
while(wrap.firstChild) wrap.removeChild(wrap.firstChild)

[torna all'indice ↑] (#TOC)

// jQuery
var parent = $('#about').parent()

// javascript
var parent = document.getElementById('about').parentNode
// jQuery
if($('#wrap').is(':empty'))

// javascript
if(!document.getElementById('wrap').hasChildNodes())
// jQuery
var nextElement = $('#wrap').next()

// javascript
var nextElement = document.getElementById('wrap').nextSibling

[torna all'indice ↑] (#TOC)

Richiesta GET

// jQuery
$.get('//example.com', function (data) {
  // code
})

// javascript
var httpRequest = new XMLHttpRequest()
httpRequest.onreadystatechange = function (data) {
  // code
}
httpRequest.open('GET', url)
httpRequest.send()

Richiesta POST

// jQuery
$.post('//example.com', { username: username }, function (data) {
  // code
})

// javascript
var httpRequest = new XMLHttpRequest()
httpRequest.onreadystatechange = function (data) {
  // code
}
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
httpRequest.open('POST', url)
httpRequest.send('username=' + encodeURIComponent(username))

[torna all'indice ↑] (#TOC)

// jQuery
$.getJSON('//openexchangerates.org/latest.json?callback=?', function (data) {
  // code
})

// javascript
function success(data) {
  // code
}
var scr = document.createElement('script')
scr.src = '//openexchangerates.org/latest.json?callback=formatCurrency'
document.body.appendChild(scr)

[torna all'indice ↑] (#TOC)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment