Programmation et le codage des applications JavaScript, les applications Web sont de plus en plus facile, avec le ferme appui de puissants frameworks JavaScript comme jQuery, MooTools Toutefois, dans le cadre de cette Free Article JavaScript tutorial, L'auteur ne traite que les
- Demo
- Agrandir
- Recharger
- New window
G�n�rez vos vid�os d'entreprise par l'IA avec la voix ou simplement du texte
Votre premi�re application vid�o AI GRATUITE ! Automatisez votre premi�re vid�o AI. Cr�ez votre vid�o professionnelle en 5 minutes gr�ce � l'IA. Aucun �quipement ni comp�tence en montage vid�o requis. Production vid�o sans effort pour les sp�cialistes du marketing de contenu.
Javascript is becoming much more accessible thanks to libraries such as jQuery & Mootools. In this article, hopefully to form part of a new series, we are going to take a look behind the libraries - not looking at the code of the libraries but revealing how much easier they make our lives.
Adding Events - Easy with Libraries!
Today we're going to be looking at adding events. With jQuery, it's as simple as:
1 | $( '#header' ).click( function () {...}); |
However with normal javascript it's not so nice. As you might expect, Firefox, Safari, Chrome, Opera and similar use one set (the correct, I might add) way of doing it, whereas Internet Explorer (all of them!) use a different code. That means for each event we add, we need to write the code twice. No worries though. I'm going to show you both sets of code and then we'll create our own function to abstract the code away and let us write one set of code which will work with every browser.
Events without the Library
Firstly, we'll set up just a simple function which we will run when an element is clicked.
1 | function doSomething() |
2 | { |
3 | alert( 'you clicked me!' ); |
4 | } |
And an element for us to select:
1 | < h1 id = "header" >This is what we'll be clicking on!</ h1 > |
Select the Element for Clicking!
So our first step (and this works in all browsers, thankfully!) we will just select the header element:
1 | var h1 = document.getElementById( 'header' ); |
Firefox, Safari, Opera, Chrome and so on.
So this is the code that all popular, modern day browsers use. It's incredibly straight forward and is called addEventListener():
1 | h1.addEventListener( 'click' , doSomething, false ); |
The function takes 3 arguments. The first is the event - when you want the function to be run. We've used click, but there are countless others. The second is the function itself - we created this function earlier. Note that because we are passing the function through as an argument we don't need to add brackets. The third is to do with javascript bubbling and when the event is fired. This is something which is a bit too complex right now, but we'll cover it in the future!
Internet Explorer
IE's code is pretty similar - oh Microsoft, why not just use the same as everyone else?
1 | h1.attachEvent( 'onclick' , doSomething); |
This only takes two arguments, the event and the function to execute. Whereas with most browsers you would use 'click', this time it's 'onclick'. Simply put, add 'on' to any event.
If you run the respective code in the relevant browser, it will work. If you use the wrong code, check, and it wont work.
Our own Version.
So, this is a pain right? What we are going to do now is create our own function which will allow us to only write the addEvent code once, not twice. I'll show you all the code and using the comments you should be able to see most of what is going on:
01 | function addEvent(elem, evt, func, cap) |
02 | { |
03 |
04 | if (elem.attachEvent) |
05 | { |
06 | //if this evaluates to true, we are working with IE so we use IE's code. |
07 | elem.attachEvent( 'on' +evt, func); |
08 | } else { |
09 | //the statement has evaluated to false, so we are not in IE/ |
10 | //the capture argument is optional. If it's left out, we set it to false: |
11 | if (!cap) cap = false ; |
12 | //and use the normal code to add our event. |
13 | elem.addEventListener(evt, func, cap); |
14 | } |
15 | } |
And the usage of this is straight forward:
1 | addEvent(h1, 'click' , doSomething, false ); |
And We are Done
I hope you enjoyed this, it's always good to step back from the library and write some of our own functions.
- Sent (0)
- Nouveau
Générez vos vidéos d'entreprise par l'IA avec la voix ou simplement du texte
chatGPTaz.com
Parlez à ChatGPT dans votre langue maternelle
AppAIVidéo
Votre première application vidéo AI GRATUITE
Deepfake Video
Deepfake AI Video Maker
Deepfake
Deepfake AI Video Maker
AI Deep Fake
Deepfake AI Video Maker
AIvidio
AI Video Mobile Solutions
AIvideos
AI Video Platform & Solutions
AIvedio
AI Video App Maker
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
Artificial General Intelligence
Ai and higher level Artificial General Intelligence (AGI)
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Faceswap AI en ligne
Échangez des visages, des vidéos, des photos et des GIF instantanément avec de puissants outils d'IA - Faceswap AI Online GRATUIT
Powerful AI Presentation PPT Maker for FREE
Build an impressive presentation with our free online AI presentation app
Your next top AI Assistant
Claude AI, developed by Anthropic
Your next top AI Assistant
Claude AI, developed by Anthropic
Temu gratuit 500 $ pour les nouveaux utilisateurs
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Crédits publicitaires TikTok gratuits
Maîtrisez les publicités TikTok pour le marketing de votre entreprise
Dall-E-OpenAI.com
Générez automatiquement des images créatives avec l'IA
chatGPT4.win
Parlez à ChatGPT dans votre langue maternelle
Premier produit d'intelligence artificielle d'Elon Musk - Grok/UN.com
Parlez au chatbot Grok AI dans votre langue
Outily.win
Centre d'outils ouvert et gratuit, utilisable par tous et pour tous, avec des centaines d'outils
GateIO.gomymobi.com
Airdrops gratuits à réclamer et à partager jusqu'à 150 000 $ par projet
iPhoneKer.com
Économisez jusqu'à 630 $ à l'achat d'un nouvel iPhone 16
Acheter le robot Tesla Optimus
Commandez votre robot Tesla Bot : Optimus Gen 2 dès aujourd'hui pour moins de 20 000 $