`
jian0487
  • 浏览: 96180 次
  • 性别: Icon_minigender_1
  • 来自: 宁德
社区版块
存档分类
最新评论

jQuery:DOM部分之Attributes 的使用

阅读更多

在jQuery函数系列介绍完之后,将会介绍一些比较实用的例子,供大家参考,敬请期待......

函数:addClass(class)

功能:给每个匹配的元素添加类

返回:jQuery对象

参数:类名

例子:

jQuery Code

$("p").addClass("selected")

Before

<p>Hello</p>

Result:

[ <p class="selected">Hello</p> ]



jQuery Code

$("p").addClass("selected highlight")

Before

<p>Hello</p>

Result:

[ <p class="selected highlight">Hello</p> ]


函数:attr(name)

功能:获取匹配对象指定属性的属性值

返回:Object

参数:要获取的属性名称

例子:

Returns the src attribute from the first image in the document.



jQuery Code

$("img").attr("src");

Before

<img src="test.jpg"/>

Result:

test.jpg


函数:attr(properties)

功能:给匹配对象设定一系列的属性值

返回:jQuery对象

参数:key/value对的属性对象

例子:

Sets src and alt attributes to all images.



jQuery Code

$("img").attr({ src: "test.jpg", alt: "Test Image" });

Before

<img/>

Result:

<img src="test.jpg" alt="Test Image"/>


函数:attr(key, value)

功能:给匹配对象设定某个属性值

返回:jQuery对象

参数:

key (String): The name of the property to set. 

value (Object): The value to set the property to.



例子:

Sets src attribute to all images.



jQuery Code

$("img").attr("src","test.jpg");

Before

<img/>

Result:

<img src="test.jpg"/>


函数:attr(key, value)

功能:和上面一样,只不过这里的value值是一个函数的返回值

返回:jQuery对象

参数:

key (String): The name of the property to set. 

value (Function): A function returning the value to set. Scope: Current element, argument: Index of current element 

例子:

Sets title attribute from src attribute.



jQuery Code

$("img").attr("title", function() { return this.src });

Before

<img src="test.jpg" />

Result:

<img src="test.jpg" title="test.jpg" />



Enumerate title attribute.



jQuery Code

$("img").attr("title", function(index) { return this.title + (i + 1); });

Before

<img title="pic" /><img title="pic" /><img title="pic" />

Result:

<img title="pic1" /><img title="pic2" /><img title="pic3" />


函数:html()

功能:获取匹配元素的html内容(innerHTML)

返回:String

例子:

jQuery Code

$("div").html();

Before

<div><input/></div>

Result:

<input/>
函数:html(val)

功能:给匹配元素设定innerHTML属性

返回:jQuery对象

参数:val (String): Set the html contents to the specified value. 

例子:

jQuery Code

$("div").html("<b>new stuff</b>");

Before

<div><input/></div>

Result:

<div><b>new stuff</b></div>

函数:removeAttr(name)

功能:移除匹配元素的指定属性

返回:jQuery对象

参数:name (String): The name of the attribute to remove. 

例子:

jQuery Code

$("input").removeAttr("disabled")

Before

<input disabled="disabled"/>

Result:

<input/>

函数:removeClass(class)

功能:移除匹配元素的指定类

返回:jQuery对象

参数:class (String): (optional) One or more CSS classes to remove from the elements 

例子:

jQuery Code

$("p").removeClass()

Before

<p class="selected">Hello</p>

Result:

[ <p>Hello</p> ]



jQuery Code

$("p").removeClass("selected")

Before

<p class="selected first">Hello</p>

Result:

[ <p class="first">Hello</p> ]



jQuery Code

$("p").removeClass("selected highlight")

Before

<p class="highlight selected first">Hello</p>

Result:

[ <p class="first">Hello</p> ]

函数:text()

功能:获取匹配元素的text内容(innerText)

返回:String

例子:

Gets the concatenated text of all paragraphs



jQuery Code

$("p").text();

Before

<p><b>Test</b> Paragraph.</p><p>Paraparagraph</p>

Result:

Test Paragraph.Paraparagraph

[code]函数:text(val)
功能:设定匹配元素的innerText属性,val中的<>将会被会转义成实体符号
返回:String
参数:val (String): The text value to set the contents of the element to. 
例子:
Sets the text of all paragraphs.

jQuery Code
$("p").text("<b>Some</b> new text.");
Before
<p>Test Paragraph.</p>
Result:
<p><b>Some</b> new text.</p>

Sets the text of all par
分享到:
评论

相关推荐

    JQUERY技术内幕:深入解析QUERY架构设计与实现原理 完整版 共两个包

    深入解析jquery架构设计与实现原理》首先通过...最后详细分析了功能模块的源码实现,包括:属性操作attributes、事件系统events、dom遍历traversing、dom操作manipulation、样式操作css、异步请求ajax、动画effects

    PHP DOM扩展库:SimpleXML 解析XML文档.md

    为了确保一致性的用户体验,可能需要使用兼容性技巧或库(如jQuery)。 - **与CSS和JavaScript的交互**:DOM允许JavaScript脚本与HTML文档进行交互,并可以访问和修改文档的CSS样式,这为创建复杂动态网页提供了...

    jQuery.in.Action.3rd.Edition.161729207

    In it, you'll learn how to traverse the DOM, handle events, perform animations, write jQuery plugins, perform Ajax requests, and even unit test your code. Its unique Lab Pages anchor each concept in ...

    jQuery技术内幕 深入解析jQuery架构设计与实现原理

    最后详细分析了功能模块的源码实现,包括:属性操作attributes、事件系统events、dom遍历traversing、dom操作manipulation、样式操作css、异步请求ajax、动画effects。  《jquery技术内幕:深入解析jquery架构设计...

    jQuery Attributes(属性)的使用(一、属性篇)

    在JavaScript的世界里,jQuery是一个非常流行的库,它简化了DOM操作、事件处理、动画效果以及Ajax交互等任务。本文将重点介绍jQuery中的属性操作,帮助你更好地理解和运用这些功能。 首先,我们来看jQuery中用于...

    jquery技术内幕:深入解析jquery架构设计与实现原理 完整版第二个包

    深入解析jquery架构设计与实现原理》首先通过...最后详细分析了功能模块的源码实现,包括:属性操作attributes、事件系统events、dom遍历traversing、dom操作manipulation、样式操作css、异步请求ajax、动画effects

    jQuery源码分析之init的详细介绍

    通常使用 document.querySelectorAll 来获取一组 DOM 元素,然后将这些元素作为数组存储在 jQuery 对象中,同时设置 jQuery 对象的 length 属性。在实现链式调用时,如果涉及到只能对单个 DOM 对象操作的方法(如 ...

    使用递归和dom遍历dom树形结构

    ### 使用递归与DOM遍历DOM树形结构 在现代Web开发中,处理XML文档是一项常见任务。DOM(Document Object Model)是一种广泛采用的技术,用于表示XML或HTML文档的结构,使得开发者能够轻松地访问、修改文档中的元素...

    jquery各版本,jquery1.3-1.7,jquery文件

    jQuery1.6的主要改进是分离了DOM元素的选择与操作,创建了独立的$.attr和$.prop方法,以更准确地区分属性(attributes)和特性(properties)。这使得开发者在处理表单控件状态等特性时有了更清晰的区分。 jQuery...

    jQuery类库文档 包括 api, js

    jQuery,作为一款广泛使用的JavaScript库,极大地简化了DOM操作、事件处理、动画制作以及Ajax交互。这个压缩包包含了jQuery的核心知识,包括API文档和实际的js文件,帮助开发者深入理解并高效运用jQuery。 **1. ...

    web技术DOM

    jQuery是广泛使用的JavaScript库,简化了DOM操作和事件处理。例如,jQuery的`$(selector).html()`可以轻松地更改匹配选择器元素的HTML内容,而`$(document).ready(function() { ... })`确保在DOM加载完成后执行代码...

    jquery-3.2.0.js,jquery-3.2.0.min.js

    **jQuery 是一个广泛使用的 JavaScript 库,它极大地简化了 JavaScript 的 DOM 操作、事件处理、动画设计以及Ajax交互。在本篇文章中,我们将深入探讨 jQuery 的核心特性、优点以及如何在实际项目中应用这两个文件:...

    jQuery显示图片详情

    同时,合理使用jQuery的选择器和操作方法,减少DOM操作的次数,也是优化性能的关键。 综上所述,"jQuery显示图片详情"这个特效涵盖了jQuery的核心概念和技术,包括事件处理、DOM操作、CSS控制、动画效果以及响应式...

    jQuery1.10.3中文文档

    以上只是jQuery1.10.3中部分核心功能的概述。通过深入学习和实践,开发者可以利用这个版本的jQuery创建功能丰富、用户体验优秀的Web应用。这份中文文档和速查表对于初学者和有经验的开发者来说都是宝贵的参考资料,...

    JQuery1.4.1类库,JQuery1.4.2类库和帮助文档

    4. **API详解**:jQuery的API包括选择器(Selectors)、属性操作(Attributes)、DOM操作(Manipulation)、CSS操作(CSS)、事件处理(Events)、动画(Effects)和Ajax交互(Ajax)等多个方面。例如,`$(selector)...

    jquerymobile所需要的库文件

    6. **事件处理**:使用 jQuery Mobile 专门的事件,如 `pageinit`、`pagebeforechange` 和 `pageshow`,来替代传统的 jQuery DOM 加载事件。 7. **性能优化**:对于大型应用,考虑使用页面碎片(page caching)和...

    Beyond.jQuery.1484222342

    Beyond jQuery gives you the confidence to abandon your jQuery crutches and walk freely with the power of the "web API" and JavaScript! Learn about the most important concepts surrounding web ...

    JQueryMobile UI 汇总

    **jQuery Mobile UI 概述** jQuery Mobile 是一个用于构建响应式和触摸友好的网页应用的框架,它基于 jQuery 和 jQuery UI。这个框架专门设计用来优化在各种设备,包括智能手机、平板电脑和桌面电脑上的表现,使得...

    jquery监控数据是否发生改变

    虽然不是jQuery的一部分,但可以与jQuery结合使用: ```javascript var observer = new MutationObserver(function(mutationsList) { for(var mutation of mutationsList) { if (mutation.type === 'childList') ...

Global site tag (gtag.js) - Google Analytics