<script src="http://code.jquery.com/jquery-3.2.1.js"></script> or.. <script src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> or.. <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>The benefit of this approach is that the user may have already cached the library from browsing other sites that use the same library. This saves bandwidth and results in a speed increase.
<script src="../js/jquery-3.2.1.min.js"></script>The benefit of downloading to your own site is that you don't rely on the availability of 3rd party sites (CDN) and can be used in situations without network connection.
<!DOCTYPE html> <html> <head> <title>jQuery</title> <script src="http://code.jquery.com/jquery-3.2.1.js"></script> <script> // javascript or jquery here </script> </head> <body> Hello World <br> </body> </html>
<!DOCTYPE html> <html> <head> <title>Document demo</title> </head> <body> <p> This file loaded: </p> <div id="timestamp"> </div> </body> </html>
<script type="text/javascript"> var timestamp = document.getElementById("timestamp"); timestamp.innerHTML = Date(); timestamp.style.backgroundColor = "yellow"; </script>
document.getElementById(); // returns an single element document.getElementsByTagName(); // returns an array document.getElementsByClassName(); // returns an array
var anchors = document.getElementsByTagName('a'); for(var i=0; i < anchors.length; i++){ anchors[i].style.color = "yellow"; }
$('a').css("color", "red");
// <h1> tag h1 { color: blue; background: yellow; } // <em> elements nested inside the <p> element p em { color: blue; } // CSS selector for all members of the class "important" // eg. <span class="important"> .important { font-weight: bold; } // CSS selector for any <p> element in the class "important" // eg. <p class="important"> p.important { color: red; } // CSS selector for element with id "newest" // eg. <li id="important"> #newest { font-weight: bold; } // CSS selector for <div> element with id "newest" // eg. <div id="important"> div#newest { color: red; }
// <h1> tag $('h1') // <em> elements nested inside the <p> element $('p em') // CSS selector for all members of the class "important" // eg. <span class="important"> $('.important') // CSS selector for any <p> element in the class "important" // eg. <p class="important"> $('p.important') // CSS selector for element with id "newest" // eg. <li id="important"> $('#newest') // CSS selector for <div> element with id "newest" // eg. <div id="important"> $('div#newest')
$('h1').append(" Javascript Was Here"); $('.statistics').css("font-style","italic"); $('.institutions').css("color","green"); $('#reed').html('Yooo hoooooooooo!');
$('li:even')Remember that computers start counting from 0 and since jQuery selection is like an array with a list of elements. :even will index into the selection using even numbers (0, 2, 4..) which will actually be the first, third, and fifth items in the selection.
$('p:first') // first paragraph $('p:last') // last paragraph
$("a:not('.navButton')") // <a> tag except ones with a class of navButton
$('li:has(a)') // <li> that has <a> tag inside
$("a:contains('Click Me!')") // find <a> that says "Click Me!"
$('div:hidden').show(); // find and show all hidden <div> tags
$('div:visible').hide(); // find and hide all visible <div> tags
<script> var anchors = document.getElementsByTagName('a'); for(var i=0; i < anchors.length; i++){ // go through anchors[0] - anchors[anchors.length] anchors[i].style.display = "none"; } </script>
<script> $('a').hide(); </script>
$('#popup').width(300); $('#popup').height(300);you can chain them in jQuery like this:
$('#popup').width(300).height(300);
$('#popup').width(300).height(300).text('Hi!').fadeIn(1000);
<p> Hello World! </p> <script> $('p').html("<strong>Yo</strong> Universe!"); alert($('p').html()); </script>
<div class="demo-container"> <div>Demonstration Box</div> <ul> <li>list item 1</li> <li>list <strong>item</strong> 2</li> </ul> </div> <script> document.write( $("div.demo-container" ).text() ); //$( "div.demo-container" ).text( "<p>This is a test.</p>" ); </script>
<ul> <li> one </li> <li> two </li> <li> three </li> <li> four </li> </ul> <script> $('ul').append('<li> five </li>'); </script>
<ul> <li> one </li> <li> two </li> <li> three </li> <li> four </li> </ul> <script> $('ul').prepend('<li> zero </li>'); </script>
<ul> <li> one </li> <li> two </li> <li> three </li> <li> four </li> </ul> <script> $('li:first').remove(); $('li:last').replaceWith('4'); </script>