jQuery prop() 方法

jQuery 教程 · 2019-03-27 16:58:59

实例

添加并移除名为 "color" 的属性:

$("button").click(function(){ var $x = $("div"); $x.prop("color","FF0000"); $x.append("The color 属性: " + $x.prop("color")); $x.removeProp("color"); });

定义和用法

prop() 方法设置或返回被选元素的属性和值。

当该方法用于返回属性值时,则返回第一个匹配元素的值。

当该方法用于设置属性值时,则为匹配元素集合设置一个或多个属性/值对。

注意:prop() 方法应该用于检索属性值,例如 DOM 属性(如 selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, 和 defaultSelected)。

提示:如需检索 HTML 属性,请使用 attr() 方法代替。

提示:如需移除属性,请使用 removeProp() 方法。

语法

返回属性的值:

  
    $(selector).prop(property)

设置属性和值:

  
    $(selector).prop(property,value)

使用函数设置属性和值:

  
    $(selector).prop(property,function(index,currentvalue))

设置多个属性和值:

  
    $(selector).prop({property:value, property:value,...})

参数 描述
property 规定属性的名称。
value 规定属性的值。
function(index,currentvalue) 规定返回要设置的属性值的函数。
  • index - 检索集合中元素的 index 位置。
  • currentvalue - 检索被选元素的当前属性值。

点击查看所有 jQuery 教程 文章: https://codercto.com/courses/l/35.html

查看所有标签

Programming From The Ground Up

Programming From The Ground Up

Jonathan Bartlett / Bartlett Publishing / 2004-07-31 / USD 34.95

Programming from the Ground Up is an introduction to programming using assembly language on the Linux platform for x86 machines. It is a great book for novices who are just learning to program as wel......一起来看看 《Programming From The Ground Up》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具