jQuery常用函数_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > jQuery常用函数

jQuery常用函数

 2011/10/19 8:04:56  itoracja  http://itoracja.iteye.com  我要评论(0)
  • 摘要:<p><spanstyle="font-family:simsun;line-height:23px;"><pre><div><spanstyle="line-height:18px;color:#000000;">attribute:$(”p”).addclass(css中定义的样式类型);给某个元素添加样式$(”img”).attr({src:”test.jpg”,alt:”testimage”});给某个元素添加属性<
  • 标签:常用 函数 jQuery
    <p><span style="font-family: simsun; line-height: 23px;"><pre><div><span style="line-height: 18px; color: #000000;">attribute:$(”p”).addclass(css中定义的样式类型); 给某个元素添加样式$(”img”).attr({src:”test.jpg”,alt:”test image”}); 给某个元素添加属性</span><span style="line-height: 18px; color: #000000;">/</span><span style="line-height: 18px; color: #000000;">值,参数是map</span><span style="line-height: 18px; color: #000000;"></span><span style="line-height: 18px; color: #000000;">$(”img”).attr(”src”,”test.jpg”); 给某个元素添加属性</span><span style="line-height: 18px; color: #000000;">/</span><span style="line-height: 18px; color: #000000;">值</span><span style="line-height: 18px; color: #000000;"></span><span style="line-height: 18px; color: #000000;">$(”img”).attr(”title”, </span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">() { </span><span style="line-height: 18px; color: #0000ff;">return</span><span style="line-height: 18px; color: #000000;"> </span><span style="line-height: 18px; color: #0000ff;">this</span><span style="line-height: 18px; color: #000000;">.src }); 给某个元素添加属性</span><span style="line-height: 18px; color: #000000;">/</span><span style="line-height: 18px; color: #000000;">值</span><span style="line-height: 18px; color: #000000;"></span><span style="line-height: 18px; color: #000000;">$(”元素名称”).html(); 获得该元素内的内容(元素,文本等)$(”元素名称”).html(”</span><span style="line-height: 18px; color: #000000;"><</span><span style="line-height: 18px; color: #000000;">b</span><span style="line-height: 18px; color: #000000;">></span><span style="line-height: 18px; color: #0000ff;">new</span><span style="line-height: 18px; color: #000000;"> stuff</span><span style="line-height: 18px; color: #000000;"><</span><span style="line-height: 18px; color: #000000;">/</span><span style="line-height: 18px; color: #000000;">b>”); 给某元素设置内容</span><span style="line-height: 18px; color: #000000;"></span><span style="line-height: 18px; color: #000000;">$(”元素名称”).removeattr(”属性名称”) 给某元素删除指定的属性以及该属性的值$(”元素名称”).removeclass(”class”) 给某元素删除指定的样式$(”元素名称”).text(); 获得该元素的文本$(”元素名称”).text(value); 设置该元素的文本值为value$(”元素名称”).toggleclass(class) 当元素存在参数中的样式的时候取消,如果不存在就设置此样式$(”input元素名称”).val(); 获取input元素的值$(”input元素名称”).val(value); 设置input元素的值为valuemanipulation:$(”元素名称”).after(content); 在匹配元素后面添加内容$(”元素名称”).append(content); 将content作为元素的内容插入到该元素的后面$(”元素名称”).appendto(content); 在content后接元素$(”元素名称”).before(content); 与after方法相反$(”元素名称”).clone(布尔表达式) 当布尔表达式为真时,克隆元素(无参时,当作true处理)$(”元素名称”).empty() 将该元素的内容设置为空$(”元素名称”).insertafter(content); 将该元素插入到content之后$(”元素名称”).insertbefore(content); 将该元素插入到content之前$(”元素”).prepend(content); 将content作为该元素的一部分,放到该元素的最前面$(”元素”).prependto(content); 将该元素作为content的一部分,放content的最前面$(”元素”).remove(); 删除所有的指定元素$(”元素”).remove(”exp”); 删除所有含有exp的元素$(”元素”).wrap(”html”); 用html来包围该元素$(”元素”).wrap(element); 用element来包围该元素traversing:add(expr)add(html)add(elements)children(expr)contains(str)end()filter(expression)filter(filter)find(expr)is(expr)next(expr)not(el)not(expr)not(elems)parent(expr)parents(expr)prev(expr)siblings(expr)core:$(html).appendto(”body”) 相当于在body中写了一段html代码$(elems) 获得dom上的某个元素$(</span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">(){……..}); 执行一个函数$(”div </span><span style="line-height: 18px; color: #000000;">></span><span style="line-height: 18px; color: #000000;"> p”).css(”border”, “1px solid gray”); 查找所有div的子节点p,添加样式$(”input:radio”, document.forms[</span><span style="line-height: 18px; color: #000000;">0</span><span style="line-height: 18px; color: #000000;">]) 在当前页面的第一个表单中查找所有的单选按钮$.extend(prop) prop是一个jquery对象,举例:jquery.extend({min: </span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">(a, b) { </span><span style="line-height: 18px; color: #0000ff;">return</span><span style="line-height: 18px; color: #000000;"> a </span><span style="line-height: 18px; color: #000000;"><</span><span style="line-height: 18px; color: #000000;"> b </span><span style="line-height: 18px; color: #000000;">?</span><span style="line-height: 18px; color: #000000;"> a : b; },max: </span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">(a, b) { </span><span style="line-height: 18px; color: #0000ff;">return</span><span style="line-height: 18px; color: #000000;"> a </span><span style="line-height: 18px; color: #000000;">></span><span style="line-height: 18px; color: #000000;"> b </span><span style="line-height: 18px; color: #000000;">?</span><span style="line-height: 18px; color: #000000;"> a : b; }});jquery( expression, [context] ) —$( expression, [context]); 在默认情况下,$()查询的是当前html文档中的dom元素。each( callback ) 以每一个匹配的元素作为上下文来执行一个函数举例:</span><span style="line-height: 18px; color: #000000;">1</span><span style="line-height: 18px; color: #000000;">$(”span”).click(</span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">){$(”li”).each(</span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">(){$(</span><span style="line-height: 18px; color: #0000ff;">this</span><span style="line-height: 18px; color: #000000;">).toggleclass(”example”);});});举例:</span><span style="line-height: 18px; color: #000000;">2</span><span style="line-height: 18px; color: #000000;">$(”button”).click(</span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;"> () {$(”div”).each(</span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;"> (index, domele) {</span><span style="line-height: 18px; color: #008000;">//</span><span style="line-height: 18px; color: #008000;"> domele == this</span><span style="line-height: 18px; color: #008000;"></span><span style="line-height: 18px; color: #000000;">$(domele).css(”backgroundcolor”, “yellow”);</span><span style="line-height: 18px; color: #0000ff;">if</span><span style="line-height: 18px; color: #000000;"> ($(</span><span style="line-height: 18px; color: #0000ff;">this</span><span style="line-height: 18px; color: #000000;">).is(”#stop”)) {$(”span”).text(”stopped at div index #” </span><span style="line-height: 18px; color: #000000;">+</span><span style="line-height: 18px; color: #000000;"> index);</span><span style="line-height: 18px; color: #0000ff;">return</span><span style="line-height: 18px; color: #000000;"> </span><span style="line-height: 18px; color: #0000ff;">false</span><span style="line-height: 18px; color: #000000;">;}});});jquery event:ready(fn); $(document).ready()注意在body中没有onload事件,否则该函数不能执行。在每个页面中可以有很多个函数被加载执行,按照fn的顺序来执行。bind( type, [data], fn ) 为每一个匹配元素的特定事件(像click)绑定一个或多个事件处理器函数。可能的事件属性有:blur, focus, load, resize, scroll, unload, click, dblclick, mousedown, mouseup, mousemove,mouseover, mouseout, mouseenter, mouseleave, change, select, submit, keydown, keypress,keyup, errorone( type, [data], fn ) 为每一个匹配元素的特定事件(像click)绑定一个或多个事件处理器函数。在每个对象上,这个事件处理函数只会被执行一次。其他规则与bind()函数相同。trigger( type, [data] ) 在每一个匹配的元素上触发某类事件。triggerhandler( type, [data] ) 这一特定方法会触发一个元素上特定的事件(指定一个事件类型),同时取消浏览器对此事件的默认行动unbind( [type], [data] ) 反绑定,从每一个匹配的元素中删除绑定的事件。$(”p”).unbind() 移除所有段落上的所有绑定的事件$(”p”).unbind( “click” ) 移除所有段落上的click事件hover( over, out ) over,out都是方法, 当鼠标移动到一个匹配的元素上面时,会触发指定的第一个函数。当鼠标移出这个元素时,会触发指定的第二个函数。$(”p”).hover(</span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">(){$(</span><span style="line-height: 18px; color: #0000ff;">this</span><span style="line-height: 18px; color: #000000;">).addclass(”over”);},</span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">(){$(</span><span style="line-height: 18px; color: #0000ff;">this</span><span style="line-height: 18px; color: #000000;">).addclass(”out”);});toggle( fn, fn ) 如果点击了一个匹配的元素,则触发指定的第一个函数,当再次点击同一元素时,则触发指定的第二个函数。$(”p”).toggle(</span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">(){$(</span><span style="line-height: 18px; color: #0000ff;">this</span><span style="line-height: 18px; color: #000000;">).addclass(”selected”);},</span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">(){$(</span><span style="line-height: 18px; color: #0000ff;">this</span><span style="line-height: 18px; color: #000000;">).removeclass(”selected”);});元素事件列表说明注:不带参数的函数,其参数为可选的 fn。jquery不支持form元素的reset事件。事件 描述 支持元素或对象blur( ) 元素失去焦点 a, input, textarea, button, select, label, map, areachange( ) 用户改变域的内容 input, textarea, selectclick( ) 鼠标点击某个对象 几乎所有元素dblclick( ) 鼠标双击某个对象 几乎所有元素error( ) 当加载文档或图像时发生某个错误 window, imgfocus( ) 元素获得焦点 a, input, textarea, button, select, label, map, areakeydown( ) 某个键盘的键被按下 几乎所有元素keypress( ) 某个键盘的键被按下或按住 几乎所有元素keyup( ) 某个键盘的键被松开 几乎所有元素load( fn ) 某个页面或图像被完成加载 window, imgmousedown( fn ) 某个鼠标按键被按下 几乎所有元素mousemove( fn ) 鼠标被移动 几乎所有元素mouseout( fn ) 鼠标从某元素移开 几乎所有元素mouseover( fn ) 鼠标被移到某元素之上 几乎所有元素mouseup( fn ) 某个鼠标按键被松开 几乎所有元素resize( fn ) 窗口或框架被调整尺寸 window, iframe, framescroll( fn ) 滚动文档的可视部分时 windowselect( ) 文本被选定 document, input, textareasubmit( ) 提交按钮被点击 formunload( fn ) 用户退出页面 windowjquery ajax 方法说明:load( url, [data], [callback] ) 装入一个远程html内容到一个dom结点。$(”#feeds”).load(”feeds.html”); 将feeds.html文件载入到id为feeds的div中$(”#feeds”).load(”feeds.php”, {limit: </span><span style="line-height: 18px; color: #000000;">25</span><span style="line-height: 18px; color: #000000;">}, </span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">(){alert(”the last </span><span style="line-height: 18px; color: #000000;">25</span><span style="line-height: 18px; color: #000000;"> entries </span><span style="line-height: 18px; color: #0000ff;">in</span><span style="line-height: 18px; color: #000000;"> the feed have been loaded”);});jquery.get( url, [data], [callback] ) 使用get请求一个页面。$.get(”test.cgi”, { name: “john”, time: “2pm” }, </span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">(data){alert(”data loaded: ” </span><span style="line-height: 18px; color: #000000;">+</span><span style="line-height: 18px; color: #000000;"> data);});jquery.getjson( url, [data], [callback] ) 使用get请求json数据。$.getjson(”test.js”, { name: “john”, time: “2pm” }, </span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">(json){alert(”json data: ” </span><span style="line-height: 18px; color: #000000;">+</span><span style="line-height: 18px; color: #000000;"> json.users[</span><span style="line-height: 18px; color: #000000;">3</span><span style="line-height: 18px; color: #000000;">].name);});jquery.getscript( url, [callback] ) 使用get请求javascript文件并执行。$.getscript(”test.js”, </span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">(){alert(”script loaded and executed.”);});jquery.post( url, [data], [callback], [type] ) 使用post请求一个页面。ajaxcomplete( callback ) 当一个ajax请求结束后,执行一个函数。这是一个ajax事件$(”#msg”).ajaxcomplete(</span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">(request, settings){$(</span><span style="line-height: 18px; color: #0000ff;">this</span><span style="line-height: 18px; color: #000000;">).append(”</span><span style="line-height: 18px; color: #000000;"><</span><span style="line-height: 18px; color: #000000;">li</span><span style="line-height: 18px; color: #000000;">></span><span style="line-height: 18px; color: #000000;">request complete.</span><span style="line-height: 18px; color: #000000;"><</span><span style="line-height: 18px; color: #000000;">/</span><span style="line-height: 18px; color: #000000;">li>”);</span><span style="line-height: 18px; color: #000000;"></span><span style="line-height: 18px; color: #000000;">});ajaxerror( callback ) 当一个ajax请求失败后,执行一个函数。这是一个ajax事件$(”#msg”).ajaxerror(</span><span style="line-height: 18px; color: #0000ff;">function</span><span style="line-height: 18px; color: #000000;">(request, settings){$(</span><span style="line-height: 18px; color: #0000ff;">this</span><span style="line-height: 18px; color: #000000;">).append(”</span><span style="line-height: 18px; color: #000000;"><</span><span style="line-height: 18px; color: #000000;">li</span><span style="line-height: 18px; color: #000000;">></span><span style="line-height: 18px; color: #000000;">error requesting page ” </span><span style="line-height: 18px; color: #000000;">+</span><span style="line-height: 18px; color: #000000;"> settings.url </span><span style="line-height: 18px; color: #000000;">+</span><span style="line-height: 18px; color: #000000;"> “</span><span style="line-height: 18px; color: #000000;"><</span><span style="line-height: 18px; color: #000000;">/</span><span style="line-height: 18px; color: #000000;">li>”);</span><span style="line-height: 18px; color: #000000;"></span><span style="line-height: 18px; color: #000000;">});</span>
发表评论
用户名: 匿名