`

Jquery Useful Attribute Methods

阅读更多

 

Useful Attribute Methods:

Following table lists down few useful methods which you can use to manipulate attributes and properties:

Methods Description
attr( properties ) Set a key/value object as properties to all matched elements.
attr( key, fn ) Set a single property to a computed value, on all matched elements.
removeAttr( name ) Remove an attribute from each of the matched elements.
hasClass( class ) Returns true if the specified class is present on at least one of the set of matched elements.
removeClass( class ) Removes all or the specified class(es) from the set of matched elements.
toggleClass( class ) Adds the specified class if it is not present, removes the specified class if it is present.
html( ) Get the html contents (innerHTML) of the first matched element.
html( val ) Set the html contents of every matched element.
text( ) Get the combined text contents of all matched elements.
text( val ) Set the text contents of all matched elements.
val( ) Get the input value of the first matched element.
val( val ) Set the value attribute of every matched element if it is called on <input> but if it is called on <select> with the passed <option> value then passed option would be selected, if it is called on check box or radio box then all the matching check box and radiobox would be checked.

Similar to above syntax and examples, following examples would give you understanding on using various attribute methods in different situation:

  • $("#myID").attr("custom") : This would return value of attribute custom for the first element matching with ID myID.

  • $("img").attr("alt", "Sample Image"): This sets the alt attribute of all the images to a new value "Sample Image".

  • $("input").attr({ value: "", title: "Please enter a value" }); : Sets the value of all <input> elements to the empty string, as well as sets the title to the string Please enter a value.

  • $("a[href^=http://]").attr("target","_blank"): Selects all links with an href attribute starting with http:// and set its target attribute to _blank

  • $("a").removeAttr("target") : This would remove target attribute of all the links.

  • $("form").submit(function() {$(":submit",this).attr("disabled", "disabled");}); : This would modify the disabled attribute to the value "disabled" while clicking Submit button.

  • $("p:last").hasClass("selected"): This return true if last <p> tag has associated classselected.

  • $("p").text(): Returns string that contains the combined text contents of all matched <p> elements.

  • $("p").text("<i>Hello World</i>"): This would set "<I>Hello World</I>" as text content of the matching <p> elements

  • $("p").html() : This returns the HTML content of the all matching paragraphs.

  • $("div").html("Hello World") : This would set the HTML content of all matching <div> to Hello World.

  • $("input:checkbox:checked").val() : Get the first value from a checked checkbox

  • $("input:radio[name=bar]:checked").val(): Get the first value from a set of radio buttons

  • $("button").val("Hello") : Sets the value attribute of every matched element <button>.

  • $("input").val("on") : This would check all the radio or check box button whose value is "on".

  • $("select").val("Orange") : This would select Orange option in a dropdown box with options Orange, Mango and Banana.

  • $("select").val("Orange", "Mango") : This would select Orange and Mango options in a dropdown box with options Orange, Mango and Banana.


分享到:
评论

相关推荐

    Multiple Attribute Decision Making Methods and Applications 2011

    5. 多属性效用理论(Multi-Attribute Utility Theory, MAUT):这种方法基于效用函数的概念,考虑决策者对于不同结果的偏好程度,并将其转化为效用值,用以决策。 6. 多准则决策分析(Multi-Criteria Decision ...

    Multiple Attribute Decision Making — Methods and Applications1981

    Multiple Attribute Decision Making — Methods and Applications A State-of-the-Art Survey 多属性决策-方法和应用最先进的调查

    jQuery中[attribute*=value]选择器用法实例

    主要介绍了jQuery中[attribute*=value]选择器用法,实例分析了[attribute*=value]选择器的功能、定义及匹配给定的属性包含某些值的元素的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

    jQuery中[attribute^=value]选择器用法实例

    本文实例讲述了jQuery中[attribute^=value]选择器用法。分享给大家供大家参考。具体分析如下: 此选择器能够选取属性值是以某些值开始的元素。 语法结构: 代码如下:$(“[attribute^=value]”) 参数列表: 参数 ...

    Uncertain Multi-Attribute Decision Making Methods and Applications 2015

    首先,多属性决策(Multi-attribute Decision Making,MADM),又称为有限选择的多目标决策,是现代决策科学的一个重要组成部分。MADM的核心目的在于从一组有限的备选方案中选择出最理想的方案。在现实生活中,人们...

    jQuery中[attribute=value]选择器用法实例

    在jQuery中,`[attribute=value]`选择器是一种强大的工具,用于选取具有特定属性和该属性值相等的HTML元素。这个选择器基于CSS选择器的语法,使得开发者能够更精确地定位页面上的元素,从而进行操作或应用样式。本文...

    jQuery中[attribute!=value]选择器用法实例

    主要介绍了jQuery中[attribute!=value]选择器用法,实例分析了[attribute!=value]选择器匹配不包含指定属性元素的使用技巧,具有一定的参考借鉴价值,需要的朋友可以参考下

    最新版JQuery-jquery-3.2.1.min.js

    - **选择器**:使用CSS选择器,如`$("#id")`,`.class`,`[attribute=value]`等,可以方便地选取DOM元素。 - **DOM操作**:`append()`, `prepend()`, `before()`, `after()`, `html()`, `text()`, `val()`等方法用于...

    jquery总结学习资料JQuery总结,jquery总结学习资料JQuery总结,jquery总结学习资料JQuery总结

    jQuery 是一个广泛使用的JavaScript库,由John Resig于2006年创建,它极大地简化了JavaScript编程,尤其是处理DOM操作、事件处理、动画效果和Ajax交互。jQuery 的设计目标是“write less, do more”,它通过提供简洁...

    jquery-3.3.1.js和jquery.min-3.3.1.js

    - **选择器**:jQuery提供了丰富的选择器,如ID选择器(`#id`)、类选择器(`.class`)、属性选择器(`[attribute]`)等,使得选取DOM元素变得简单。 - **DOM操作**:jQuery封装了DOM操作,如`$(selector).html()`, `$...

    jquery文本,以文本格式保存的jquery文件

    例如,`attr("attributeName", "value")`用于设置属性,而`attr("attributeName")`则用于获取属性值。jQuery还提供了`removeAttr()`方法来移除属性。 **样式和CSS**在jQuery中通常是通过`.css()`方法处理的。你可以...

    jquery1.7及其帮助文档

    2. **`.attr()` 和 `.prop()` 的区分:** 为了更好地处理属性和特性,`.attr()` 和 `.prop()` 分别被用来获取或设置元素的属性(attribute)和特性(property)。`.attr()` 主要用于HTML属性,`.prop()` 用于...

    jquery1.6.3与jquery1.9.0包

    《jQuery 1.6.3与jQuery 1.9.0:从过去到现代的JavaScript奇迹》 在JavaScript的世界里,jQuery是一个不可或缺的库,它极大地简化了DOM操作、事件处理、动画效果以及Ajax交互。这里我们将深入探讨jQuery 1.6.3和...

    Graph Theory and Fuzzy Multiple Attribute Decision Making Methods 2007

    Decision Making in the Manufacturing Environment Using Graph Theory and Fuzzy Multiple Attribute Decision Making Methods 利用图论和模糊多属性决策方法在制造环境下进行决策

    Python Attributes and Methods

    例如,`object.attribute`,这里的`attribute`就是`object`的一个属性。你可以将属性视为连接不同对象的桥梁,通过它可以从一个对象访问到另一个对象。此外,你也可以通过赋值操作来创建新的属性,如`object.new_...

    jquery资料--jquery学习资料

    2. **属性操作(Attribute Manipulation)**:`.attr()`用于获取或设置元素属性,`.data()`则处理数据相关的属性。 3. **内容操作(Content Manipulation)**:`.html()`, `.text()`, `.val()`分别用于获取或设置...

    jQuery 1.6 中文API (适用jQuery 1.6,jQuery 1.6.1,jQuery 1.6.2)

    `.attr()`方法用于获取或设置元素的属性(attribute),而`.prop()`方法则用于处理元素的特性(property)。这一改动使得开发者能够更精确地处理HTML元素的属性和JavaScript对象的特性,避免了混淆。 在事件处理...

    jQuery中文API

    1. **选择器(Selectors)**:jQuery的核心在于其强大的选择器,它支持CSS1到CSS3的选择器,如ID选择器(#id),类选择器(.class),元素选择器(element),属性选择器([attribute])等,让开发者能够快速精准地定位到...

    jquery1.7 API/jquery-1.7.1.min.js/jquery-1.7.1.js

    jQuery 1.7版本支持CSS1至CSS3的选择器,如ID选择器(#id),类选择器(.class),属性选择器([attribute])等,还有jQuery特有的如`:first`,`:last`,`:even`,`:odd`等伪类选择器。 5. **jQuery事件**: jQuery提供...

    Manning jQuery in Action.pdf

    - **Extending jQuery**: Developers can extend the functionality of jQuery by adding custom methods to the jQuery prototype. - **Using jQuery with Other Libraries**: jQuery integrates seamlessly with ...

Global site tag (gtag.js) - Google Analytics