首先我们看看手册上的描述
load(url, params, callback)
装入一个远程HTML内容到一个DOM结点。
注意:避免用装入的scripts脚本,装入脚本改用$.getScript.当任何字符显示时,IE会忽略所有的脚本。
下面我们看看下面的代码
$("#btndemo1").click(function(){
$("#demo1").load("loadhtml.html");
})
当点击 btndemo1按钮时候 将会把loadhtml.html的内容加载到 id=demo1 的div里面
这个很简单大家都容易理解下面我们看看下面的代码
$("#btndemo2").click(function(){
$("#demo2").load("loadhtml.html #div2");
})
当点击 btndemo1按钮时候 将会把loadhtml.html的内容里面 id=div2 元素里面的内容 加载到 id=demo1 的div里面
loadhtml.html #div2注意中间一定要有空格
所有代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery基础教程六(滑动效果函数之slideDown,slideToggle和slideUp)</title>
<script language="javascript" src=" $("#demo1").load("loadhtml.html");
})
$("#btndemo2").click(function(){
$("#demo2").load("loadhtml.html #div2");
})
})
//--><br%20/>%0D%0A</SCRIPT><br%20/>%0D%0A</head><br%20/>%0D%0A<body></font></strong></p>%0D%0A<p><strong><font%20color=%22#ff0000%22><input%20type=%22button%22%20name=%22Submit%22%20value=%22%E5%8A%A0%E5%9C%A8loadhtml.html%22%20ID=" btndemo1> <div ID="demo1" class="redborder"><br />
点击上面按钮将执行:
<br />
<br />
$("#btndemo1").click(function(){<br />
$("#demo1").load("loadhtml.html"); <br />
})</div>
<input type="button" name="Submit" value="加在loadhtml.html 中的ID=div2 的内容" ID='btndemo2'/>
<div ID="demo2" class="redborder"><br />
点击上面按钮将执行:
<br />
<br />
$("#btndemo1").click(function(){<br />
$("#demo1").load("loadhtml.html"); <br />
})</div>
loadhtml.html的代码是<br />
<br />
<div ID="div1">div1</div><br />
<div ID="div2">div2</div>
</body>
</html>
演示地址:http://www.cnjquery.com/demo/jquery基础教程八 load方法及小技巧.html