jQuery基础

Basic Structure

$(document).ready(function(){
    $(selector).event(function(){
        $(selector).effect();
    });
    $(document).on('selector', 'event', function(){
        $(selector).effect();
    });
});

selector and select child\parent element

$('element_type')
$('.class')
$('#element_id')
$('selector:nth-child(4)')
$('selector:last-child(4)')
$('selector:even')
$('selector:odd')
$('selector').parent()
$(this)

event

- mouseenter(function(){})
- mouseleave(function(){})
- click()
- dblclick()

double click

- hover(inFunc, outFunc)

inFunction is required.

- focus()
- keydown() keypress() keyup()

keydown: the key is on its way down
keypress: the key is pressed down
keyup: the key is released

effect

- slideDown(‘fast’)
- slideToggle(‘slow’)
- fadeTo(‘fast’, 0.25)
- hide()
- fadeOut()
- fadeIn()
- animate()

input1: the animation to perform
input2: the time in which to perform the animation

$(document).ready(function() {
   $('div').animate({left:'+=10px'},500);
   //increasing the distance from the left margin moves something to the right
});

Modifying HTML Elements

- target.append(‘<p>element</p>’)

inserts the specified element as the last child of the target element.

- prepend()

inserts the specified element as the first child of the target element.

- appendTo(target)
- prependTo()
- move element

ele1.after(ele2);
ele1.before(ele2);

- empty()

deletes an element’s content and all its descendants

- remove()

not only deletes an element’s content, but deletes the element itself

- addClass()
- removeClass()
- toggleClass()

if the element has the class, it removes that class; if not, it adds the class.

- height(value) / width(value)
- css(CSS_element, value)
- html()

get/set html content

- val()

get the value of form element

$('input:checkbox:checked').val();
//Get the value from an input
var input = $('input[name=checkListItem]').val();
- clone()
- prop(property, value)

set element property.
There is also a method attr(), but I am quite confused about html attr vs. prop.

Using Ajax get and display json file example

getJSON()
$.getJSON("/json/cats.json", function(json) {
  $(".message").html(JSON.stringify(json));
});
Convert Json data to HTML
var html = "";
json.forEach(function(val) {
  var keys = Object.keys(val);
  html += "<div class = 'cat'>";
  keys.forEach(function(key) {
    html += "<strong>" + key + "</strong>: " + val[key] + "<br>";
  });
  html += "</div><br>";
});
$(".message").html(html);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值