Linux Driver

2016年7月10日 星期日

前端網頁技術(1): JQuery 入門

<script src=""></script>

$( "p" ).on({
    "click": function() { console.log( "clicked!" ); },
    "mouseover": function() { console.log( "hovered!" ); }



<h2>This is a heading</h2>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
Email: <input type="text" name="email">


jQuery Selectors

網頁操作的對象, HTML tag, id, .class, this

Demonstrates the jQuery hide() method, hiding all <p> elements.
Demonstrates the jQuery hide() method, hiding the element with id="test".
Demonstrates the jQuery hide() method, hiding all elements with class="test".
Demonstrates the jQuery hide() method, hiding the current HTML element.

HTML element也可以合併使用 class name, 如 $("p.test").hide( )

$(":disabled") All disabled input elements
$(":selected") All selected input elements
$(":checked") All checked input elements

jQuery Events



第二種:  // 可以方便對同一個object設多個event callback

$( "p" ).on(
{"click": function() {   $("p").text("hello"); },
 "mouseover": function() {   $("p").text("world"); }


jQuery click()
Demonstrates the jQuery click() event.
jQuery dblclick()
Demonstrates the jQuery dblclick() event.
jQuery mouseenter()
Demonstrates the jQuery mouseenter() event.
jQuery mouseleave()
Demonstrates the jQuery mouseleave() event.
jQuery mousedown()
Demonstrates the jQuery mousedown() event.
jQuery mouseup()
Demonstrates the jQuery mouseup() event.
jQuery hover()
Demonstrates the jQuery hover() event.
jQuery focus() and blur()
Demonstrates the jQuery focus() and blur() events.

jQuery Traversing

The image below illustrates a family tree. With jQuery traversing, you can easily move up (ancestors), down (descendants) and sideways (siblings) in the family tree, starting from the selected (current) element. This movement is called traversing - or moving through - the DOM.
jQuery Dimensions

The find() method returns descendant elements of the selected element, all the way down to the last descendant.
The following example returns all <span> elements that are descendants of <div>:


The DOM tree: This method traverse downwards along descendants of DOM elements, all the way down to the last descendant. To only traverse a single level down the DOM tree (to return direct children), use the children() method.

children() : Returns all direct children of the selected element
find(): Returns descendant elements of the selected element
filter() :Reduce the set of matched elements to those that match the selector or pass the function's test
each() : Executes a function for each matched element

Filer Example: 

    $("p").filter(".intro").css("background-color", "yellow");
     //$("div p").first();
  //$("div p").last();

Each Example: 


<button>Alert the value of each list item</button>


另一個寫法, 取得參數

function(index,element)  A function to run for each matched element.
      index - The index position of the selector
     element - The current element (the "this" selector can also be used)


jQuery Prop

對html element 自訂一些自有的 property

Add and remove a property named "color":
    var $x = $("div");
    $x.append("The key property: " + $x.prop("key"));

        $("#p1").html("attr('checked'): " + $("input").attr('checked123')
        + "<br>prop('checked'): " + $("input").prop('checked'));


<button>Check value of attr() and prop()</button>

<input id="check1" type="checkbox" checked123="checkedQQQ">
<label for="check1">Check me</label>
<p id="p1"></p>

$("input").prop('checked') : 會以實際checkbox有無按下做輸出true/false
$("input").attr('checked123') : 會去抓checked123這個屬性的內容checkedQQQ

The most important concept to remember about the checked attribute is that it does not correspond to the checked property. The attribute actually corresponds to the defaultChecked property and should be used only to set the initial value of the checkbox. The checked attribute value does not change with the state of the checkbox, while the checked property does. Therefore, the cross-browser-compatible way to determine if a checkbox is checked is to use the property
attr() : 這是對HTMLelement 所定義的標準 attribute

更多 jQuery Traversing Methods的Method


  2. jQuery Examples

沒有留言 :