javascript and jQuery

5 types:
js should be camelCase
number, string, boolean, null and undefined
eg: for string:

when in console typing:
"hi " + "goodbye" => "hi goodbye"

the length of string: String.length (0 based)

 var name = "jaq";
 name = "sl";
 var number = 73;
 var isRight = true;

javascript can change the value type of variable

 var n = "jaq";
 n = 15

undefined type:

 var age; // this is undefined

Test in console

clear() // remove the history
alert("Hello there!");
console.log("Hello from the console!")
prompt("What is your name?") // this will pop up a dialogue and a     
                             // place for you to type in sth
var userName = prompt("What is your name?")

connect js with html


    Script Demo
    

more examples:

var firstName = prompt("what is your first name?");
var lastName = prompt("what is your last name?");
var age = prompt("what is your age?");
console.log("Your full name is " + firstName + " " + lastName)

Equality Operators

== performs type operation, === is not
var x = 99;
x == "99" // true
x === "99" // false
"" is empty string

false values:

fase 0 "" null undefined NaN

loop in js:
var count = 1;

while(cnt < 6) {
console.log("count is: " + count);
count++
}

the function of string type

var str = "hello";
str.indexOf("w");
str.charAt(0).toUpperCase() + str.slice(1);

function in js
function singSong(length, width) {

console.log(length * width);
return "how is you?"

}

js arrays:
var friends = ["Charlie", "Liz", "David", "Mattias"]
friends[4] = "Amelie";

Arrays Last few things

var friends = []; // no friends
var friends = new Array() // uncommon
// arrays can hold any type of data
var random_array = [49, true, "Herm", null];
// can also directly add new data
var friends = ["Charlie", "a", "c"];
friends[4] = "d";

Push and Pop of array

var colors = ["a", "b", "c"];
colors.push("d");

Use pop to remove the last item in an array

var colors = ["r", "o", "y"];
var col = colors.pop();

unshift: add to the front of an array

var colors = ["r", "o", "y"];
colors.unshift("in")
// ["in", "r", "o", "y"]

shift: remove the first item in an array

colors.shift(); // ["r", "o", "y"]

use indexOf() to find the index of an item in an array
use slice() to copy parts of an array

var fruits = [1, 2, 3];
var otherNums = nums.slice(a, b);
// a included, b not included

For loop

var colors = ["a", "b", "c"];
for(var i = 0; i< colors.length; i++) {
    console.log(colors[i]);
}

javascript and jQuery_第1张图片
when multiple parameters are introduced:

  • The first one represents each element in the array (per loop iteration) that .forEach was called on.
  • The second represents the index of said element.
  • The third represents the array that .forEach was called on (it will be the same for every iteration of the loop).

javascript and jQuery_第2张图片

array.splice(startIdx, how many, ele1, ele2, ...)
// the start index to add/remove elements from
// 2nd element: the # of elements to remove
// ele1, ele2, ... the elements to add to the array

Retrieve Data: bracket and dot notatioon
javascript and jQuery_第3张图片
javascript and jQuery_第4张图片
javascript and jQuery_第5张图片
function in function
javascript and jQuery_第6张图片
javascript and jQuery_第7张图片
javascript and jQuery_第8张图片

document.querySelector() // 使用方法
// get the first 

element in the document with class="example" document.querySelector("p.example"); // change the text of an element with id="demo" document.querySelector("#demo").innerHTML = "Hello World!"; // Get the first

element in the document where the parent is a

element. document.querySelector("div > p");

DOM Manipulation
javascript and jQuery_第9张图片
javascript and jQuery_第10张图片

javascript and jQuery_第11张图片
test and add style class to


javascript and jQuery_第12张图片
clipboard.png

p.classList.toggle(className)
// boolean type used to see if already have the className
p.classList.remove(className)
// remove className from the classList of p

textContent
javascript and jQuery_第13张图片

use getAttribute() and setAttribute() to read and write attributes like src or href

javascript and jQuery_第14张图片
clipboard.png
for(var i=0; i< links.length; i++) {

console.log(links[i].textContent);

}
for(var i=0; i< links.length; i++) {

links[i].style.background = "pink";

}
for(var i = 0; i < links.length; i++) {

links[i].style.border = "1px dashed purple";
links[i].style.color = "orange";

}
for(var i = 0; i< links.length; i++) {

console.log(links[i].getAttribute("href");

}

javascript and jQuery_第15张图片
javascript and jQuery_第16张图片
for example:

document.querySelector("ul").addEventListener("click", function(){
    console.log("UL WAS CLICKED!");
});
var lis = document.querySelectorAll("li");
lis
for(var i = 0; i < lis.length; i++) {
    lis[i].addEventListener("click", function(){
        this.style.color = "pink";
    });
}

javascript and jQuery_第17张图片
javascript and jQuery_第18张图片
// change the background color

javascript and jQuery_第19张图片
var button = document.querySelector("button");
var isPurple = false;
button.addEventListener("click", function(){

document.body.classList.toggle("purple");

});
"mouseOver" used when you want to change sth when the mouse is hanging over sth.

javascript and jQuery_第20张图片

jQuery : make dom easier
javascript and jQuery_第21张图片
$('h1')[0];
// change the color to be yellow
$('h1').css('color', 'yellow')
// if not using jquery, then use:
document.querySelector("h1").style.color = "orange";
var lis = document.querySelectorAll("li");
for(var i = 0; i< lis.length; i++){

lis[i].style.color = "green";

}
$("li").css({

fontSize: "10px",
border: "3px dashed purple",
background: "rgba(89, 45, 20, 0.5)"

});
jQuery 用法
javascript and jQuery_第22张图片
$("h1").text("New Text!!!"); // change the text to be "New Text!!!"
clipboard.png
// can change the attribute type to be like "checkbox", "color"...
javascript and jQuery_第23张图片
$("img").css("width", "200px");
$("img").last().attr("src", "https://www.baidu.com");

javascript and jQuery_第24张图片
text-decoration: line-through;

click()
// prints when item with id 'submit' is clicked
$('#submit').click(function(){

console.log("Another click");

});
// alerts when ANY button is clicked
$('button').click(function(){

alert("Someone clicked a button");
$(this).css("background", "pink")

});
$("button").click(function(){

var text = $(this).text();
console.log(" You clicked " + text);

});
// on() it's not just for click events. on() supports all types of events
// double click event
$('button').on('dblclick', function(){

 alert("Double Click!");

});
javascript and jQuery_第25张图片
$("button").on("mouseenter", function(){ // also can use "mouseleave"

$(this).css("font-weight", "bold");

});

javascript and jQuery_第26张图片
// fade in and fade out
// use fadeToggle to directly detect whether it is shown on screen,
// if no then fadein if yes then fadeout
// slidedown(), slideToggle(1000, function(){ });
javascript and jQuery_第27张图片

你可能感兴趣的:(jquery,javascript)