`
e_soft
  • 浏览: 48802 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

jquery in action2的学习笔记(二)

阅读更多


二:Bringing pages to life
with jQuery

<img id="myImage" src="image.gif" alt="An image" class="someClass"
title="This is an image" data-custom ="some value"/>

1:As an example of using the each() method, we could use the following code to set the
id property of every element in the DOM to a name composed of the element’s tag
name and position within the DOM:

$('*').each(function(n){
this.id = this.tagName + n;
});


2:We can retrieve that attribute’s value, as if it were any of the standard
attributes

$("#myImage").attr("data-custom")


3:We can set that attribute’s value

$('*').attr('title',function(index,previousValue) {
return previousValue + ' I am element ' + index +
' and my name is ' + (this.id || 'unset');
});


another way:

$('input').attr(
{ value: '', title: 'Please enter a value' }
);


4:FORCING LINKS TO OPEN IN A NEW WINDOW

$("a[href^='http://']").attr("target","_blank");


5:SOLVING THE DREADED DOUBLE-SUBMIT PROBLEM
For the client side of the solution (the server-side code should still be written in a
paranoid fashion), we’ll hook into the form’s submit event and disable the submit
button after its first press.

$("form").submit(function() {
$(":submit",this).attr("disabled", "disabled");
});


6:Adding and removing class names

addClass removeClass
toggleClass Adds the specified class name if it doesn’t exist on an element, or removes the name from
elements that already possess the class name. Note that each element is tested individually, so
some elements may have the class name added, and others may have it removed.

function swapThem() {
$('tr').toggleClass('striped');
}

$(function(){/
$("table tr:nth-child(even)").addClass("striped");
$("table").mouseover(swapThem).mouseout(swapThem);
});


7:Another commonly desired ability is to obtain the list of classes defined for a particular
element as an array instead of the cumbersome space-separated list. We could
try to achieve that by writing

$("p:first").attr("className").split(" ");


Recall that the attr() method will return undefined if the attribute in question
doesn’t exist, so this statement will throw an error if the <p> element doesn’t possess
any class names.

We could solve this by first checking for the attribute

$.fn.getClassNames = function() {
var name = this.attr("className");
if (name != null) {
return name.split(" ");
}
else {
return [];
}
};


8:expand the width of all elements
in the wrapped set by 20 pixels as follows:

$("div.expandable").css("width",function(index, currentWidth) {
return currentWidth + 20;
});

9:GETTING AND SETTING DIMENSIONS

$("div.myElements").width(500)
is identical to
$("div.myElements").css("width",500)

$(function(){
        $(window).resize(displayDimensions);
        displayDimensions();
});

function displayDimensions() {
        $('#display').html(
          $('#testSubject').width()+'x'+$('#testSubject').height()
);


10:html()
Obtains the HTML content of the first element in the matched set.
html(content)
Sets the passed HTML fragment as the content of all matched elements.
text()
Concatenates all text content of the wrapped elements and returns it as the result of the method.
text(content)
Sets the text content of all wrapped elements to the passed value. If the passed text contains
angle brackets (< and >) or the ampersand (&), these characters are replaced with their equivalent
HTML entities.

11:Appends the passed HTML fragment or elements to the content of all matched elements.

$('p').append('<b>some text<b>');


This statement moves all links with the class appendMe to the end of the child list of all
<p> elements with the class appendToMe. If there are multiple targets for the operation,
the original element is cloned as many times as is necessary and appended to the
children of each target. In all cases, the original is removed from its initial location.

$("p.appendToMe").append($("a.appendMe"))


Prepends the passed HTML fragment or elements to the content of all matched elements.
prepend(content)

Inserts the passed HTML fragment or elements into the DOM as a sibling of the target elements,
positioned before the targets. The target wrapped elements must already be part of the DOM.
before(content)

Inserts the passed HTML fragment or elements into the DOM as a sibling of the target elements
positioned after the targets. The target wrapped elements must already be part of the DOM.
after(content)


Adds all elements in the wrapped set to the end of the content of the specified target(s).
appendTo(targets)

Adds all elements in the wrapped set to the beginning of the content of the specified target(s).
prependTo(targets)

Adds all elements in the wrapped set to the DOM just prior to the specified target(s).
insertBefore(targets)

Adds all elements in the wrapped set to the DOM just after the specified target(s).
insertAfter (targets)


$('<p>Hi there!</p>').insertAfter('p img');


12:To wrap each link with the class surprise in a <div> with the class hello, we could
write

$("a.surprise").wrap("<div class='hello'></div>")


Wraps the elements of the matched set, as a unit, with the passed HTML tags or a clone of the
passed element.
wrapAll(wrapper)

13:Removes all elements in the wrapped set from the page DOM.
remove(selector)

Removes all elements in the wrapped set from the page DOM, retaining any bound events and
jQuery data.
detach(selector)

The detach() method is the preferred means of removing an element that we’ll want
to put back into the DOM at a later time with its events and data intact.

13:This statement makes copies of all image elements and appends them to all <fieldset>
elements with the class name photo.
$('img').clone().appendTo('fieldset.photo');


This statement performs the same operation as the previous example, but after the
insertion of the clones, the end() method is used to select the original wrapped set
(the original targets) and hide them.

$('ul').clone().insertBefore('#here').end().hide();



14:replace all images on the
page that have alt attributes with <span> elements that contain the alt values of the
images. Employing each() and replaceWith() we could do it like this:

$('img[alt]').each(function(){
$(this).replaceWith('<span>'+ $(this).attr('alt') +'</span>')
});


15:Returns the value attribute of the first element in the matched set. When the element is a multiselect
element, the returned value is an array of all selections.
Attention:This method also doesn’t distinguish between the
checked or unchecked states of checkboxes and radio buttons, and will simply return
the value of checkboxes or radio buttons as defined by their value attribute, regardless
of whether they’re checked or not.

val()

This expression returns the value of the single checked radio button (or undefined if
none is checked)

$('[name="radioGroup"]:checked').val()


var checkboxValues = $('[name="checkboxGroup"]:checked').map(
function(){ return $(this).val(); }
).toArray();

16:This statement will search all the <input> and <select> elements on the page for values
that match any of the input strings: one, two, or three. Any checkboxes or radio buttons
that are found to match will become checked, and any options that match will
become selected.

$('input,select').val(['one','two','three']);
0
0
分享到:
评论

相关推荐

    《JQuery in Action》学习笔记

    **jQuery 简介** jQuery 是一款高效、简洁的 JavaScript 库,它的出现极大地简化了网页的DOM操作、事件处理、动画设计以及Ajax交互。...通过深入学习和实践,开发者能够利用jQuery创造出更加互动和富媒体的网页体验。

    JQuery自学学习笔记

    ### JQuery自学学习笔记 #### 一、JQuery简介与基本语法 JQuery 是一款轻量级的 JavaScript 库,极大地简化了 HTML 元素的选择、操作以及事件处理等任务。它提供了一种更加简单的方式来进行网页开发,使得开发者...

    JQuery学习笔记(技术文档)

    ### JQuery学习笔记(技术文档) #### 一、JQuery能做什么? JQuery 是一款轻量级的 JavaScript 库,它的设计宗旨是“write less, do more”,即“写得更少,做得更多”。通过JQuery,可以实现以下功能: 1. **...

    jQuery验证框架学习笔记.pdf

    ### jQuery验证框架学习笔记知识点概览 #### 一、引言 jQuery 验证框架是基于 jQuery 开发的一个用于前端表单验证的强大插件。它提供了丰富的验证规则和灵活的配置选项,使得开发者能够轻松地实现对用户输入数据的...

    struts2学习笔记黑马程序员

    ### Struts2学习笔记之文件上传与Ajax开发 #### Struts2文件上传 **文件上传简介** 文件上传是Web应用中常见的功能之一,Struts2框架内置了对文件上传的支持,使得开发者能够轻松地实现这一功能。为了确保文件...

    jquery学习笔记及常用函数封装.zip

    《jQuery学习笔记及常用函数封装》 在编程领域,jQuery是一个广泛使用的JavaScript库,它极大地简化了DOM操作、事件处理、动画制作以及Ajax交互。本资料包“jquery学习笔记及常用函数封装.zip”包含了从JQuery基础...

    jQuery学习笔记

    ### jQuery学习笔记 #### 第一章:jQuery入门 1. **什么是jQuery?** - **定义**:jQuery是一个跨平台的开源JavaScript库,其主要目标是简化HTML文档遍历、事件处理、动画以及Ajax交互等任务。jQuery的核心理念...

    张龙圣思园struts2学习笔记word

    张龙圣思园的Struts2学习笔记,无疑为Java开发者提供了一份宝贵的参考资料,它可能涵盖了Struts2的基础概念、核心组件、配置方式以及实战技巧。 首先,让我们深入了解Struts2的核心特性。Struts2是MVC(Model-View-...

    jquery完全笔记 带目录 高清 经典前端js笔记 前端必看

    ### jQuery完全笔记知识点概述 #### 1. jQuery概述 jQuery是一个快速、小型且功能丰富的JavaScript库,它简化了HTML文档遍历、事件处理、动画和Ajax交互。通过使用jQuery,可以更方便地在网页中操作文档对象、选择...

    struts2学习资料,笔记

    Struts2是一个强大的Java EE应用程序框架,主要用于构建企业级的Web应用。它是基于MVC(Model-View-Controller)设计...Struts2 学习笔记.doc应该包含了关于这些知识点的详细讲解和示例,是学习Struts2的良好参考资料。

    CRM系统学习笔记.rar

    这个压缩包“CRM系统学习笔记.rar”包含了作者在学习CRM系统过程中积累的知识点,特别提到了Servlet和jQuery的使用。下面将对这两个关键知识点进行详细的解释。 Servlet是Java Web开发中的一个核心组件,主要用于...

    js+ajax+jquery笔记

    - **优势**:jQuery的API易于学习和使用,能有效减少跨浏览器的兼容性问题,提供强大的选择器来选取DOM元素,以及流畅的动画效果。 4. **JavaScript函数**: - **定义函数**:有三种主要方式定义JavaScript函数,...

    struts2.1.8学习笔记

    2. **配置文件**:`struts.xml`是Struts2的核心配置文件,它定义了Action类、Action的映射、结果类型等。在Struts2.1.8中,开发者需要在此文件中配置Action的执行路径、输入输出结果等信息。 3. **拦截器...

    struts2学习笔记五(第5讲.Struts2的输入校验)

    在Struts2的学习过程中,第五讲主要探讨的是输入验证,这是确保应用程序数据安全和准确性的关键步骤。输入验证通常发生在客户端和服务器端,防止恶意数据的提交,并确保用户输入的数据符合预设的格式和规则。 首先...

    界面框架extjs学习笔记

    EXTJS in Action 是一本专为学习EXTJS编写的书籍,但这里我们主要关注学习笔记中的关键知识点。 首先,EXTJS的目录结构非常重要,因为它包含了所有必要的资源和源码: 1. `adapter`:这部分包含了适配器,用于将...

    struts2图表笔记

    4. 页面交互:使用jQuery或其他库发送Ajax请求到Action,接收到JSON数据后,调用ECharts的`setOption`方法更新图表。 5. 图表初始化:在HTML页面中定义一个用于显示图表的div元素,初始化ECharts实例,并设置图表的...

    struts2学习笔记六(第6讲.Struts2的输入校验续一)

    在学习Struts2输入验证的过程中,还需要理解Action上下文(ActionContext)和值栈(Value Stack)的概念。ActionContext存储了与当前请求相关的所有信息,而值栈则是将Action对象和ActionForm对象组织在一起的地方,...

    JAVA学习笔记

    【JAVA学习笔记】是一份全面记录JAVA学习过程的资料,涵盖了从基础知识到高级应用的广泛内容。这份笔记的目的是帮助读者逐步掌握JAVA编程语言,并在实际项目开发中应用所学知识。 首先,我们从`java_se.doc`开始,...

    jQuery学习笔记(1)--用jQuery实现异步通信(用json传值)具体思路

    jQuery学习笔记(1)--用jQuery实现异步通信(用json传值)具体思路的知识点: 1. jQuery库与异步通信:jQuery是一个流行的JavaScript库,它简化了JavaScript编程,使得开发者可以用更少的代码来完成复杂的效果。在异步...

    达内学习笔记

    【达内学习笔记】是一份综合性的学习资料,包含了丰富的IT知识,主要针对Java编程语言的学习路径。这份笔记集合了达内教育机构的教学精华,旨在帮助学员系统地掌握Java技术,提升开发技能。以下是各部分笔记的主要...

Global site tag (gtag.js) - Google Analytics