首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

jQuery中惯用的函数方法总结

2012-09-07 
jQuery中常用的函数方法总结div namecode$(document).ready(function(){// Your code here...});?作

jQuery中常用的函数方法总结
<div name="code">$(document).ready(function(){ // Your code here...});

?

作用 :它可以极大地提高web应用程序的响应速度。通过使用这个方法,可以在DOM载入就绪能够读取并操纵时立即调用你所绑定的函数,而99.99%的JavaScript函数都需要在那一刻执行。

bind(type,[data],fn)

?

代码

$("p").bind("click", function(){  alert( $(this).text() );}); 

?
作用 :为每一个匹配元素的特定事件(像click)绑定一个事件处理器函数。起到事件监听的作用。

?

toggle(fn,fn)


代码:

?

$("td").toggle(  function () {    $(this).addClass("selected");  },  function () {    $(this).removeClass("selected");  });

?


作用 :每次点击时切换要调用的函数。如果点击了一个匹配的元素,则触发指定的第一个函数,当再次点击同一元素时,则触发指定的第二个函数。挺有趣的一个函数,在动态实现某些功能的时候可能会用到。

(像click(),focus(),keydown()这样的事件这里就不提了,那些都是开发中比较常用到的。)


外观效果

?

?

addClass(class)和 removeClass(class) //代码 :$(".stripe tr").mouseover(function(){                 $(this).addClass("over");}).mouseout(function(){                $(this).removeClass("over");})});//也可以写成:$(".stripe tr").mouseover(function() { $(this).addClass("over") });$(".stripe tr").mouseout(function() { $(this).removeClass("over") });

?

?


作用 :为指定的元素添加或移除样式,从而实现动态的样式效果,上面的实例中实现鼠标移动双色表格的代码。

?

css(name,value)

?

代码:

?

$("p").css("color","red");

?

?

$("#btnShow").bind("click",function(event){ $("#divMsg").show() });$("#btnHide").bind("click",function(evnet){ $("#divMsg").hide() });

<p><b>Values: </b></p><form> <input type="text" name="name" value="John"/> <input type="text" name="password" value="password"/> <input type="text" name="url" value="http://ejohn.org/%22/></form>

$("p").append( $("input").map(function(){ return $(this).val();}).get().join(", ") );

[ <p>John, password, http://ejohn.org/%3C/p> ]

<p><span>Hello</span>, how are you?</p>

$("p").find("span") ;

[ <span>Hello</span> ]

<img/><img/>

$("img").attr("src","test.jpg");

<div><p>Hello</p></div>

$("div").html();

<p>Hello</p>

<p>Test Paragraph.</p>

?

$("p").wrap("<div class='wrap'></div>");

<p>Hello, <span>Person</span> <a href="#">and person</a></p>

$("p").empty();

<p></p>

$("#feeds").load("feeds.aspx", {limit: 25}, function(){ alert("The last 25 entries in the feed have been loaded"); });

<p id="results"><b>Results: </b> </p><form> <select name="single"> <option>Single</option> <option>Single2</option> </select> <select name="multiple" multiple="multiple"> <option selected="selected">Multiple</option> <option>Multiple2</option> <option selected="selected">Multiple3</option> </select><br/> <input type="checkbox" name="check" value="check1"/> check1 <input type="checkbox" name="check" value="check2" checked="checked"/> check2 <input type="radio" name="radio" value="radio1" checked="checked"/> radio1 <input type="radio" name="radio" value="radio2"/> radio2</form>

$("#results").append( "<tt>" + $("form").serialize() + "</tt>" );

$.each( [0,1,2], function(i, n){ alert( "Item #" + i + ": " + n );});//遍历数组$.each( { name: "John", lang: "JS" }, function(i, n){ alert( "Name: " + i + ", Value: " + n );//遍历对象});

<div>First</div><div>Second</div><div>Third</div><div>Fourth</div>

var arr = jQuery.makeArray(document.getElementsByTagName("div"));

FourthThirdSecondFirst

?
作用 :将类数组对象转换为数组对象。使我们可以在数组和对象之间灵活的转换。

jQuery.trim(str)


作用
:这个大家应该很熟悉,就是去掉字符串起始和结尾的空格。

小结:在实际的开发中我们可能会用到其他的方法和属性,以上只是个人认为新手初学jQuery时,必须掌握的一些方法。仅供大家学习的参考。有什么不对的高手指教。

?

?

?

?

?

转自:http://justjavac.iteye.com/blog/714667

?

?

热点排行