- 插入在body內,Js的前方
01. select item
select an item
$("#header h1");
select many items
$("button");
02. styles
get css value
console.log($("h1").css("color"));
set css value
$("h1").css("color", "green");
add class names
$("h1").addClass("big-title margin-50");
remove class name
$("h1").removeClass("big-title");
whether have class name (return boolean)
$("h1").hasClass("bid-title");
03. text
change pure text
$("h1").text("Bye");
change html text (innerHTML)
$("h1").html("<em>Bye</em>");
04. attribute
get attribute value
console.log($("img").attr("src"));
set attribute value
$("a").attr("href", "https://www.yahoo.com")
also effect on class
$("h1").attr("class")
05. Event listener
1.listen to mouse
$("h1").click(function(){
$("h1").css("color", "purple");
});
2.listen to keyboard
$(document).keypress(function(event){
console.log(event.key);
})
3.ask all items to listen
$("button").click(function(){
$("h1").css("color", "purple");
})
4.listen to other Events
$("h1").on("mouseover", function(){
$("h1").css("color", "purple");
})
06. add new element
before: add before selected element
$("h1").before("<button>New</button>")
New
Hello
after:add after selected element
$("h1").after("<button>New</button>")
Hello
Newprepend: before selected content
$("h1").prepend("<button>New</button>")
NewHello
append: add selected content
$("h1").append("<button>New</button>")
HelloNew
remove elements
$("button").remove();
07. other website animation
1.隱藏 / 顯示
$("h1").hide();
$("h1").show();
$("h1").toggle();
2.淡出 / 淡入
$("h1").fadeOut();
$("h1").fadeIn();
$("h1").fadeToggle();
3.滑出 / 滑入
$("h1").slideUp();
$("h1").slideDown();
$("h1").slideToggle();
4.自訂效果 (限number value in CSS code)
$("h1").animate({opacity: 0.5});
5.多種效果依照順序改變
$("h1").fadeOut().fadeIn().animate({opacity: 0.5});