Jquery 中load方法使用!

JavaScript05

Jquery 中load方法使用!,第1张

jquery load方法用法详解

1.load定义和用法,load() 方法通过 AJAX 请求从服务器加载数据,并把返回的数据放置到指定的元素中。jquery load是jquery ajax中的一种功能,load可以方便快速的直接加载一个页面到指定div中(html,php),并且它可以带参数。

2.还存在一个名为 load 的 jQuery 事件方法。调用哪个,取决于参数。

下面是几个例子:

1).加载一个php文件,该php文件不含传递参数

$("#myID").load("test.php")

2).加载一个php文件,该php文件含有一个传递参数

$("#myID").load("test.php",{"name" : "Adam"})

//导入的php文件含有一个传递参数,类似于:test.php?name=Adam

或者直接

$("#divResult").load("jqueryLoad?username=" + username + "&un="+$("#username").val()+"&timestamp=" + (new Date()).getTime())

3).使用 AJAX 请求来改变 div 元素的文本

$("button").click(function(){

  $("div").load('demo_ajax_load.txt')

})

onload是js的原生的事件,而load则是这个程序中名为SpriteSheet的自定义类中自定义的一个方法(或者说是自定义的事件),两者之间没有必然的联系,如果程序编写者原意,他也可以把load改成其他名字。onload则是不能改的,它是js的内部事件名。

首先你要确定你想要load的页面中,script标签放到哪里了?

如果放倒head里面是不行的,你的script标签必需放倒body里面。

此外,

$('#container').load('./ajaxload.html#ctn')

这种方式也是不行的。因为只解析了ctn里的内容

jquery文档里给了如下说明:

Script Execution

When calling .load() using a URL without a suffixed selector expression, the content is passed to .html() prior to scripts being removed. This executes the script blocks before they are discarded. If .load() is called with a selector expression appended to the URL, however, the scripts are stripped out prior to the DOM being updated, and thus are not executed. An example of both cases can be seen below:

Here, any JavaScript loaded into #a as a part of the document will successfully execute.

1

$( "#a" ).load( "article.html" )

However, in the following case, script blocks in the document being loaded into #b are stripped out and not executed:

1

$( "#b" ).load( "article.html #target" )