存档

文章标签 ‘jquery’

XPath 知识入门 – jQuery使用XPath

2010年5月11日 没有评论

jQuery可以通过类似于CSS或者xPath的语法来获取页面中的元素。在本文中,一起来比较和学习使用 XPath 1.0 和 jQuery 1.4 处理类似的任务,让您能够在必要的时候从其中一个快速转向另一个。

例子

整篇文章中,都会回过头来参考一个方便的样例 XML 文档,参见 清单 1。此书籍列表包括各种信息,比如作者、两种完全虚构的价格和书名。
清单1: 样例 XML 文档 (book.xml)
[html]

<?xml version="1.0" encoding="utf-8"?>
<catalog>
<book format="trade">
<name>Jennifer Government</name>
<author>Max Barry</author>
<price curr="CAD">15.00</price>
<price curr="USD">12.00</price>
</book>

<book format="textbook">
<name>Unity Game Development Essentials</name>
<author>Will Goldstone</author>
<price curr="CAD">52.00</price>
<price curr="USD">45.00</price>
</book>

<book format="textbook">
<name>UNIX Visual QuickPro</name>
<author>Chris Herborth</author>
<price curr="CAD">15.00</price>
<price curr="USD">10.00</price>
</book>
</catalog>

[/html]
阅读全文…

分类: javascript 标签: , ,

jQuery操作Select

2010年5月6日 没有评论

前段时间和大家分享了jQuery选择器, 今天来看一下jQuery是如何控制和操作select的。

先看下面的html代码
阅读全文…

分类: 未分类 标签: ,

强大的jQuery选择器

2010年4月2日 没有评论

用过jquery的朋友肯定会发现,jquery的选择器非常强大,在html页面进行DOM操作时,很容易的就能娶到我们要的元素。现在给大家展示一下jquery都提供了那些选择器:
阅读全文…

分类: javascript 标签: ,

jQuery框架中$.each()函数的应用

2010年4月2日 1 条评论

最近在做中国教育在线的培训机构项目,开发工作中需要做一个开课时间表, 效果图如下所示:

js-schedule

如果直接写html代码,麻烦是麻烦点,但是这个表单很好实现。这两天在看jquery的相关知识,发现其中的$.each()这个函数功能十分强大,这里先对$.each()函数做个简单介绍,然后在给大家看一下,我是如何用$.each()这个函数实现上面这个表单的。

可以代替for循环的$.each()

$.each()函数封装了十分强大的遍历功能,它可以遍历一维数组、多维数组、DOM, JSON 等等,在javascript开发过程中使用$each可以大大的减轻我们的工作量。下面看几个例子:

[js]
var arr1 = [ &quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;four&quot;, &quot;five&quot; ];
$.each(arr1, function(){
alert(this);
});
//输出:one two three four five

var arr2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
$.each(arr2, function(i, item){
alert(item[0]);
});
//输出:1 4 7

var obj = { one:1, two:2, three:3, four:4, five:5 };
$.each(obj, function(key, val) {
alert(obj[key]);
});
//输出:1 2 3 4 5

[/js]

使用$.each()实现前面提到的表单

[js]
//引入jquery,根据你的jquery文件的不同路径,做出相应的替换,
&lt;script type=&quot;text/javascript&quot; src=&quot;/js/jquery-1.3.min.js&quot;&gt;&lt;/script&gt;

var weekday = ['','周一','周二','周三','周四','周五','周六','周日'];

var day = ['','上午','下午','晚上'];

var table = document.createElement(‘table’);

$(table).attr({ width:&quot;400&quot;, border:&quot;0&quot;, cellpadding:&quot;0&quot;, cellspacing:&quot;1&quot;, bgcolor:&quot;#CCCCCC&quot;});
$.each(day, function(key, val) {
var row = table.insertRow(key);
if(key == 0) {
$.each(weekday, function(m, n) { n = n || ‘&amp;nbsp;’; $(row.insertCell(m)).html(n); });
}
else {
$.each(weekday, function(m, n) { n = !n?day[key]:’&lt;input type=&quot;checkbox&quot; name=&quot;classtime[]&quot; /&gt;’; $(row.insertCell(m)).html(n); });
}
});

//在id=calendarDiv的那个DIV中嵌入上面的代码
$(‘#calendarDiv’).append(table);

[/js]
效果如下所示:


作者: 石巍
原载: 10V
版权所有,转载时必须以超链接形式注明作者和原始出处及本声明。

本文链接: http://www.smartwei.com/jquery-each-function.html

分类: javascript 标签: ,