教培參考
教育培訓(xùn)行業(yè)知識型媒體
發(fā)布時間: 2025年05月20日 09:17
Jquery如何獲取和設(shè)置元素內(nèi)容?代碼怎么寫?jQuery 具有可操作 HTML 元素和屬性的方法,其中jQuery 非常重要的部分就是操作DOM,獲得- text()、html() 以及 val()內(nèi)容。
獲取和設(shè)置元素內(nèi)容學習目標:能夠知道獲取和設(shè)置元素內(nèi)容的操作
html方法如何使用?
jquery中的html方法可以獲取和設(shè)置標簽的html內(nèi)容。
示例代碼:
<style> .a01{ color:red; }</style><script> $(function(){ var $a = $("#link01"); var $input = $('#input01') // 獲取元素屬性 var sId = $a.prop("id"); alert(sId); // 設(shè)置元素屬性 $a.prop({"href":"http://www.baidu.com","title":'這是去到百度的鏈接',"class":"a01"}); // 獲取value屬性 // var sValue = $input.prop("value"); // alert(sValue); // 獲取value屬性使用val()方法的簡寫方式 var sValue = $input.val(); alert(sValue); // 設(shè)置value值 $input.val("222222"); })</script><a id="link01">這是一個鏈接</a><input type="text" id="input01" value="111111">
說明:給指定標簽追加html內(nèi)容使用append方法
獲取和設(shè)置元素小結(jié):獲取和設(shè)置元素的內(nèi)容使用: html方法;給指定元素追加html內(nèi)容使用: append方法。
微信掃碼關(guān)注公眾號
獲取更多考試熱門資料