1. 程式人生 > >jQuery中選擇器加尖括號的區別

jQuery中選擇器加尖括號的區別

$("img")為一個get的方法,是讀取標籤為"img"元素的屬性及設定相應的屬性;

$("<img/>")為一個set的方法,是建立一個新的標籤元素"img"並賦予相應標籤相應的屬性

舉例:$(document).ready(function(){
findDogs();
});
function findDogs(){
$.post("/finddogs",null,
function(data){ 
$.each(data,function(){
var tr  = $("<tr/>");
$("<img/>").attr("src","images/"+this.image).attr("height",60).attr("width",60).appendTo("<td/>").appendTo(tr);
            $("<td/>").html(this.name).appendTo(tr);
            $("<td/>").html(this.price).appendTo(tr);
            $("#dogtable").append(tr);
        })
},"json");
}