jQuery中Ajax的load方法详解

2020-05-23 06:19:58易采站长站整理

<%out.println(“Hello Ajax!”);%>

下面来看下jQuery中的Ajax

1.load()

  load()方法是jQuery中最为简单和常用的Ajax方法,能远程载入HTML代码并插入DOM中。

远程HTML代码:


<!DOCTYPE html>
<html>
<head lang=”en”>
    <meta charset=”UTF-8″>
    <title></title>
</head>
<body>
<div class=”comment”>
    <h6>张三:</h6>
    <p class=”para”>沙发.</p>
</div>
<div class=”comment”>
    <h6>李四:</h6>
    <p class=”para”>板凳.</p>
</div>
<div class=”comment”>
    <h6>王五:</h6>
    <p class=”para”>地板.</p>
</div>
</body>
</html>

load()载入HTML


<!DOCTYPE html>
<html>
<head lang=”en”>
    <meta charset=”UTF-8″>
    <script type=”text/javascript” src=”../../js/jquery-2.1.3.js”></script>
    <style>
        * { margin:0; padding:0;}
        body { font-size:12px;}
        .comment { margin-top:10px; padding:10px; border:1px solid #ccc;background:#DDD;}
        .comment h6 { font-weight:700; font-size:14px;}
        .para { margin-top:5px; text-indent:2em;background:#DDD;}
    </style>
    <title></title>
</head>
<body>
<input type=”button” id=”send” value=”Ajax获取” />
<div class=”comment”>已有评论</div>
<div id=”resText”></div>
</body>
<script type=”text/javascript”>
    $(function () {
        $(“#send”).click(function () {
            $(“#resText”).load(“test.html”);
        });
    })
    /**
     * jQuery中的Ajax
     *
     * jQuery对Ajax操作进行了封装,
     *  在jQuery中$.ajax()方法属于最底层的方法,
     *  第2层是load()、$.get()、$.post()方法