在我所知道的主流JavaScript库中,jQuery拥有最棒的Ajax API。最简单的Ajax调用如:
. click ( function ( ) {window. open ( this. href );return false; } ); } );
在父窗口中操作 选中IFRAME中的所有单选钮
$(window.frames["iframe1"].document).find("input[@type='radio']").attr("checked","true");
在IFRAME中操作 选中父窗口中的所有单选钮
$(window.parent.document).find("input[@type='radio']").attr("checked","true");
取消全选
$("input[@type=checkbox][@checked]").each(function(){
$(this).attr("checked", '');
});
如果选中的只有一个,取得选中的值
$("input[@type=checkbox][@checked]").eq(0).val()
jQuery('div#intro').load('/some/fragment.html');
代码以GET请求方式,从/some/fragment.html文件中获取HTML片段,并把片段装载到id="intro"的div中。
当 我第一次看到这行代码时,几乎对它没什么印象。这看起来非常简洁,但如果你想用jQuery做些更复杂的事情,比如显示Ajax装载进度,该如何做呢? jQuery为你准备了一些可自定义的事件(ajaxStart,ajaxComplete,ajaxError等等),来实现你想要的代码。同时 jQuery也提供了广泛的底层API,来实现更复杂的Ajax交互:
jQuery.get('/some/script.php', {'name': 'Simon'}, function(data) {
alert('The server said: ' + data);
}); // 以GET方式通过/some/script.php?name=Simon获取数据
jQuery.post('/some/script.php', {'name': 'Simon'}, function(data) {
alert('The server said: ' + data);
}); // 以POST方式向/some/script.php发送请求
jQuery.getJSON('/some.json', function(json) {
alert('JSON rocks: ' + json.foo + ' ' + json.bar);
}); // 从/some.json接收并解析数据,把数据转换成JSON格式
jQuery.getScript('/script.js'); // 以GET方式获取/script.js脚本并用eval()执行
http://www.nixsky.com/program/java/2008-02/393.html
选取元素:
-
jQuery('div.panel')
- 选取了所有class="panel"的div jQuery('p#intro')
- 选取了所有id="intro"的段落 jQuery('div#content a:visible')
- 选取了id="content"的div中所有可见的链接 jQuery('input[@name=email]')
- 选取了所有name="email"的输入域 jQuery('table.orders tr:odd')
-
选取了类名为“orders”的表中所有的奇数行
jQuery('a[@href^="http://"]')
- 选取了所有(以http://开头的)外部链接 jQuery('p[a]')
-
选取了所有包含一个或多个链接的段落
上述例子中,:visible和:odd是jQuery实现的扩展,很具特色。而属性的选取使用@作为标记,
其方式和XPath一样,要优于CSS2。
jQuery的这套选取语法包罗万象,有些类似正则表达式,想完全消化是需要花上一段时间的。
$(window.frames["innerIframe"].document).find ( "a[rel='external']" ). addClass ( "external" ). click ( function ( ) {window. open ( this. href );return false; } ); } );
在父窗口中操作 选中IFRAME中的所有单选钮
$(window.frames["iframe1"].document).find("input[@type='radio']").attr("checked","true");
在IFRAME中操作 选中父窗口中的所有单选钮
$(window.parent.document).find("input[@type='radio']").attr("checked","true");
取消全选
$("input[@type=checkbox][@checked]").each(function(){
$(this).attr("checked", '');
});
如果选中的只有一个,取得选中的值
$("input[@type=checkbox][@checked]").eq(0).val()