操作元素的部分:
- 元素内的 HTML
- 元素内的 文本
- 元素内的 属性
操作的动作:
- getter:当方法调用时没有携带参数时,执行 get 动作。返回匹配元素中的第一个元素
- setter:当方法调用时带有参数时,执行 set 动作。所有匹配的元素将被修改。
.text()
将返回所有匹配元素的文本
$("h1").text()
"Working with Selections"
$("h1").text("Hello World")
Object { 0: h1.entry-title, length: 1, prevObject: {…}, context: HTMLDocument https://learn.jquery.com/using-jquery-core/working-with-selections/, selector: "h1" }
getter
setter
Chaining
对于返回一个 jQuery 对象的方法($()
或 setter),可以进行链接; .end()
会回到链接的首端
$( "#content" )
.find( "h3" )
.eq( 2 )
.html( "new text for the third h3!" )
.end() // Restores the selection to all h3s in #content ,回到 $("#content")
.eq( 0 )
.html( "new text for the first h3!" );
操纵元素的方法
-
.html()
– Get or set the HTML contents. -
.text()
– Get or set the text contents; HTML will be stripped. -
.attr()
– Get or set the value of the provided attribute. -
.width()
– Get or set the width in pixels of the first element in the selection as an integer. -
.height()
– Get or set the height in pixels of the first element in the selection as an integer. -
.position()
– Get an object with position information for the first element in the selection, relative to its first positioned ancestor. This is a getter only. -
.val()
– Get or set the value of form elements.
注意!set 动作会应用于 jQuery Selection 中的所有元素!
网友评论