jquery拿取对象的方式
$(‘#id') :通过元素的id
$(‘tagname') : 通过元素的标签名
$(‘tagname tagname') : 通过元素的标签名,eg: $(‘ul li')
$(‘tagname#id): 通过元素的id和标签名
$(‘:checkbox'):拿取input的 type为checkbox'的所有元素:
<input type="checkbox" name="appetizers"
value="imperial"/>
$('span[price] input[type=text]') :拿取下面的input元素
<span price="3">
<input type="text" name="imperial.quantity"
disabled="disabled" value="1"/>
</span>
$('p',$(this).parents('p:first')):拿取该p的上(至少都是父节点)的第一个p节点
$('~ span:first',this): locates the first sibling of this that's a <span> element.
延迟加载js文件:$.getscript
例子:
html文件:
<!doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd">
<html>
<head>
<title>$.getscript example</title>
<link rel="stylesheet" type="text/css" href="../common.css">
<script type="text/javascript"
src="../scripts/jquery-1.2.1.js"></script>
<script type="text/javascript">
$(function(){
$('#loadbutton').click(function(){
$.getscript(//在firefox/3.0.1中会出现一个错误(语法错误,定义的变量不起作用,ff2没问题)
'new.stuff.js'//,function(){$('#inspectbutton').click()}
);
});
$('#inspectbutton').click(function(){
somefunction(somevariable);
test()
});
});
</script>
</head>
<body>
<button type="button" id="loadbutton">load</button>
<button type="button" id="inspectbutton">inspect</button>
</body>
</html>
js文件:
alert("i'm inline!");
var somevariable = 'value of somevariable';
function somefunction(value) {
alert(value);
}
function test() {
alert('test');
}
alert("i'm inline!");
var somevariable = 'value of somevariable';
function somefunction(value) {
alert(value);
}
function test() {
alert('test');
}
jquery数组处理:
<!doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd">
<html>
<head>
<title>hi!</title>
<script type="text/javascript" src="../scripts/jquery-1.2.1.js">
</script>
<script type="text/javascript">
var $ = 'hi!';
jquery(function(){
alert('$ = '+ $);//这里的 $ 为 hi!,把它变回jquery的符号:jquery(function($){...}/这样就可以了
//alert(jquery)
});
jquery(function($){
//------------遍历数组 .each的使用-------------
var anarray = ['one','two','three'];
$.each(anarray,function(n,value) {
//do something here
//alert(n+' '+value);
});
var anobject = {one:1, two:2, three:3};
$.each(anobject,function(name,value) {
//do something here
//alert(name+' '+value);
});
//-----------过滤数组 .grep的使用------------
var originalarray =[99,101,103];
var bignumbers = $.grep(originalarray,'a>100');//第2种写法,还可以用正则表达式来过滤
$.each(bignumbers,function(n,value) {
//do something here
//alert(n+' '+value);
});
//------------转换数组 .map的使用------------
var strings = ['1','2','3','4','s','k','6'];
var values = $.map(strings,function(value){
var result = new number(value);
return isnan(result) ? null : result;//如果result不是数字则返回null(返回null在这里相当于不返回)
});
$.each(values,function(n,value) {
//do something here
//alert(value);
});
var characters = $.map(
['this','that','other thing'],
function(value){return value.split('');}//分离字符串用返回给characters
);
//alert(characters.length);
//------------.inarray(value,array)的使用------------返回value在array下标的位置,如果value不在array中则返回
-1
var index = $.inarray(2,[1,2,3,4,5]);
//alert(index);
//------------makearray(obj)的使用------------将类数组对象转换为数组对象。
var arr = jquery.makearray(document.getelementsbytagname_r("p"));
//arr.reverse(); // 使用数组翻转函数
$.each(arr,function(n,value) {
//do something here
//alert(n+' '+value);
//alert(value.html());
});
var arr2 =$.unique(document.getelementsbytagname_r("p")); //获得唯一的对象,看api,说得很模
糊,http://docs.jquery.com/utilities/jquery.unique
alert();
$.each(arr2,function(n,value) {
//do something here
alert(n+' '+value);
});
});
</script>
</head>
<body>
<p>first</p><p>second</p><p>third</p><p>fourth</p><p>fourth</p>
</body>
</html>
以上就是jquery each拿取对象的方式实例详解的详细内容。