注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

让一切都逝去吧

排骨炖泥菜/猪肝炒苹果/鱼籽狗肉汤/狗头薏米汤

 
 
 

日志

 
 

jquery  

2011-11-13 23:01:43|  分类: web |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

jQuery.each( collection, callback(indexInArray, valueOfElement) )

collectionThe object or array to iterate over.

callback(indexInArray, valueOfElement)The function that will be executed on every object.

      each()函数是基本上所有的框架都提供了的一个工具类函数,通过它,你可以遍历对象、数组的属性值并进行处理。jQuery和jQuery对象都实 现了该方法,对于jQuery对象,只是把each方法简单的进行了委托:把jQuery对象作为第一个参数传递给jQuery的each方法.换句话 说:jQuery提供的each方法是对参数一提供的对象的中所有的子元素逐一进行方法调用。而jQuery对象提供的each方法则是对jQuery内 部的子元素进行逐个调用。

      jQuery中常用getJSON来调用并获取远程的JSON字符串,将其转换为JSON对象,如果成功,则执行回调函数。原型如下:
jQuery.getJSON( url, [data], [callback] )  跨域加载JSON数据。
url:     发送请求的地址
data : (可选) 待发送key/value参数
callback: (可选) 载入成功时的回调函数
 
主要用于客户端获取服务器JSON数据。简单示例:
服务器脚本,返回JSON数据:
$.getJSON.php
$arr=array("name"=>"zhangsan", "age"=>20);
$jarr=json_encode($arr);
echo $jarr;
注意两点:
第一:在返回客户端之前,先用PHP函数json_encode将要返回的数据进行编码。
第二:返回到客户端用的是echo,而不是return。
 
下面是核心的客户端代码:
$.getJSON.html
<script language="javascript" type="text/javascript" src="http://ljhzzyx.blog.163.com/blog/./js/jquery.js"></script>
<script language="javascript" type="text/javascript">
function getjs()
{
  $.getJSON("$.getJSON.php", {}, function(response){
                 alert(response.age);
  });
}
 
<input type="button" name="btn" id="btn" value="test" onClick="javascript:getjs();"/>
注意一点:
由于在PHP中是用JSON编码返回值,所以此处必须用getJSON去调用PHP文件,从而获取数据。同时可以注意到,经由getJSON得到的数据已经变成了一个对象数组,可以用response.name,response.age很直观的获取返回值。(http://katrina.blog.51cto.com/709863/144721)

     

.live( events, handler )


Description: Attach an event handler for all elements which match the current selector, now and in the future.

  • version added: 1.3.live( events, handler )

    eventsA string containing a JavaScript event type, such as "click" or "keydown." As of jQuery 1.4 the string can contain multiple, space-separated event types or custom event names.

    handlerA function to execute at the time the event is triggered.

  • version added: 1.4.live( events, data, handler )

    eventsA string containing a JavaScript event type, such as "click" or "keydown." As of jQuery 1.4 the string can contain multiple, space-separated event types or custom event names.

    dataA map of data that will be passed to the event handler.

    handlerA function to execute at the time the event is triggered.

  • version added: 1.4.3.live( events-map )

    events-mapA map of one or more JavaScript event types and functions to execute for them.

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().

This method provides a means to attach delegated event handlers to the document element of a page, which simplifies the use of event handlers when content is dynamically added to a page. See the discussion of direct versus delegated events in the .on() method for more information.

Rewriting the .live() method in terms of its successors is straightforward; these are templates for equivalent calls for all three event attachment methods:

$(selector).live(events, data, handler);                // jQuery 1.3+ $(document).delegate(selector, events, data, handler);  // jQuery 1.4.3+ $(document).on(events, selector, data, handler);        // jQuery 1.7+ 

The events argument can either be a space-separated list of event type names and optional namespaces, or an event-map of event names strings and handlers. The data argument is optional and can be omitted. For example, the following three method calls are functionally equivalent (but see below for more effective and performant ways to attach delegated event handlers):

$("a.offsite").live("click", function(){ alert("Goodbye!"); });                // jQuery 1.3+ $(document).delegate("a.offsite", "click", function(){ alert("Goodbye!"); });  // jQuery 1.4.3+ $(document).on("click", "a.offsite", function(){ alert("Goodbye!"); });        // jQuery 1.7+ 

Use of the .live() method is no longer recommended since later versions of jQuery offer better methods that do not have its drawbacks. In particular, the following issues arise with the use of .live():

  • jQuery attempts to retrieve the elements specified by the selector before calling the .live() method, which may be time-consuming on large documents.
  • Chaining methods is not supported. For example, $("a").find(".offsite, .external").live( ... ); is not valid and does not work as expected.
  • Since all .live() events are attached at the document element, events take the longest and slowest possible path before they are handled.
  • Calling event.stopPropagation() in the event handler is ineffective in stopping event handlers attached lower in the document; the event has already propagated to document.
  • The .live() method interacts with other event methods in ways that can be surprising, e.g., $(document).unbind("click") removes all click handlers attached by any call to .live()!

For pages still using .live(), this list of version-specific differences may be helpful:

  • Before jQuery 1.7, to stop further handlers from executing after one bound using .live(), the handler must return false. Calling .stopPropagation() will not accomplish this.
  • As of jQuery 1.4 the .live() method supports custom events as well as all JavaScript events that bubble.
  • In jQuery 1.3.x only the following JavaScript events could be bound: click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, and mouseup.

  • .clone( [withDataAndEvents] )

    withDataAndEventsA Boolean indicating whether event handlers should be copied along with the elements. As of jQuery 1.4, element data will be copied as well.

  • version added: 1.5.clone( [withDataAndEvents] [, deepWithDataAndEvents] )

    withDataAndEventsA Boolean indicating whether event handlers and data should be copied along with the elements. The default value is false*In jQuery 1.5.0 the default value was incorrectly true; it was changed back to false in 1.5.1 and up.

    deepWithDataAndEventsA Boolean indicating whether event handlers and data for all children of the cloned element should be copied. By default its value matches the first argument's value (which defaults to false).

The .clone() method performs a deep copy of the set of matched elements, meaning that it copies the matched elements as well as all of their descendant elements and text nodes. When used in conjunction with one of the insertion methods, .clone() is a convenient way to duplicate elements on a page. Consider the following HTML:

<div class="container">   <div class="hello">Hello</div>   <div class="goodbye">Goodbye</div> </div>

As shown in the discussion for .append(), normally when an element is inserted somewhere in the DOM, it is moved from its old location. So, given the code:

$('.hello').appendTo('.goodbye');

The resulting DOM structure would be:

<div class="container">   <div class="goodbye">     Goodbye     <div class="hello">Hello</div>   </div> </div>

To prevent this and instead create a copy of the element, you could write the following:

$('.hello').clone().appendTo('.goodbye');

This would produce:

<div class="container">   <div class="hello">Hello</div>   <div class="goodbye">     Goodbye     <div class="hello">Hello</div>   </div> </div>

Note that when using the .clone() method, you can modify the cloned elements or their contents before (re-)inserting them into the document.

Normally, any event handlers bound to the original element are not copied to the clone. The optional withDataAndEvents parameter allows us to change this behavior, and to instead make copies of all of the event handlers as well, bound to the new copy of the element. As of jQuery 1.4, all element data (attached by the .data() method) is also copied to the new copy.

However, objects and arrays within element data are not copied and will continue to be shared between the cloned element and the original element. To deep copy all data, copy each one manually:

var $elem = $('#elem').data( "arr": [ 1 ] ), // Original element with attached data     $clone = $elem.clone( true )     .data( "arr", $.extend( [], $elem.data("arr") ) ); // Deep copy to prevent data sharing 

As of jQuery 1.5, withDataAndEvents can be optionally enhanced withdeepWithDataAndEvents to copy the events and data for all children of the cloned element.


      <%-- JSP 隐式注释 --%>jsp里面要用这个注释啊,用<!-- 显式注释 [ <%= 表达式 %> ] -->这个不行,多年前的老知识了,不过已经忘了。

  评论这张
 
阅读(2494)| 评论(3)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017