jQuery 捕獲/設置 內容和屬性

 

捕獲

設置

val() $('#txtBox').val() $('#txtBox').val('我是個box')
text() $('#txtBox').text() $('#txtBox').text('這是個box')
html() $('#txtBox').html() $('#txtBox').html('<p>這是個box</p>')
prop() $('#txtBox').prop('class') $("#checkBox").prop("checked", true);
attr() $('#txtBox').attr('action')   //action自定義屬性

$("#txtBox").attr("href","http://www.baidu.com");

$("#txtBox").attr({

"href" : "http://www.baidu.com",

"title" : "百度一下,你就知道"

});

attr與prop的區別

【prop】  1,如果元素有該屬性,用prop捕獲時返回對應屬性值   2,如果沒有該屬性,返回空字符串

【attr】1,如果元素有該屬性,attr捕獲時返回對應的屬性值   2,若沒有該屬性,返回值是undefined

因此,我們常用prop方法處理元素的固有屬性,用attr處理我們自定義的屬性

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章