欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用jQuery的load方法設(shè)計動態(tài)加載及解決被加載頁面js失效問題

 更新時間:2017年03月01日 15:27:23   作者:七星6609  
這篇文章主要介紹了使用jQuery的load方法設(shè)計動態(tài)加載及解決被加載頁面js失效問題,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

一、問題分析

對于后臺系統(tǒng),相比大家都有所印象,知道其中的布局結(jié)構(gòu),如圖:

在這種布局中我們需要將header,sidebar,footer分開,而且對于中間部分的content內(nèi)容需要動態(tài)變化,即根據(jù)不同菜單定位到不同頁面,而整體布局不會變化

這種布局結(jié)構(gòu)對于單純的HTML不具備這種嵌入各部分內(nèi)容的能力,所以就需要我們自己來尋找或者解決這種問題,由于jquery的兼容性和使用廣度比較不錯,這里

使用jquery的load方法來處理這種頁面布局框架。

二、load方法詳解

1.定義

  $(selector).load(URL,data,callback);

  必需的 URL 參數(shù)規(guī)定您希望加載的 URL。

  可選的 data 參數(shù)規(guī)定與請求一同發(fā)送的查詢字符串鍵/值對集合。

  可選的 callback 參數(shù)是 load() 方法完成后所執(zhí)行的函數(shù)名稱。

 2.示例

也可以把 jQuery 選擇器添加到 URL 參數(shù)。

    下面的例子把 "demo_test.txt" 文件中 id="p1" 的元素的內(nèi)容,加載到指定的 <div> 元素中:

    $("#div1").load("demo_test.txt #p1");

可選的 callback 參數(shù)規(guī)定當(dāng) load() 方法完成后所要允許的回調(diào)函數(shù)。回調(diào)函數(shù)可以設(shè)置不同的

$("#div1").load("demo_test.txt",function(responseTxt,statusTxt,xhr){
 if(statusTxt=="success")
  alert("外部內(nèi)容加載成功!");
 if(statusTxt=="error")
  alert("Error: "+xhr.status+": "+xhr.statusText);
 });

三、布局框架load的使用

1.問題

  網(wǎng)上很多人在使用load方法加載動態(tài)頁面的時候遇到一個普遍的問題,就是在被加載頁面中的JavaScript代碼失效,這是因為load加載的外部文件會把Script部分刪除掉,所以被加載頁面中調(diào)用該頁面的JavaScript的時候就會出現(xiàn)xxxfunction未定義。

2.解決

對于header,sidebar,footer這種只包含靜態(tài)HTML代碼的部分直接使用load加載

對應(yīng)中間content變化的內(nèi)容,一般都會包含對應(yīng)的JavaScript代碼,使用自定義的load方法(如下代碼),在使用jquery.load()方法加載對應(yīng)的內(nèi)容的同時,使用load的回調(diào)方法處理JavaScript的加載,將被加載頁面的JavaScript代碼加載到布局頁面的<div id="content"></div>中這樣每次load()的時候content的內(nèi)容都會被覆蓋,所以也不必?fù)?dān)心重復(fù)加載的問題。這樣就完美解決被加載頁面js失效的問題。具體代碼如下所示:

四、代碼示例

布局頁面:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title></title>
  <!-- Tell the browser to be responsive to screen width -->
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  <!-- Bootstrap 3.3.6 -->
  <link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css" rel="external nofollow" >
 </head>
 <body class="hold-transition skin-blue-light sidebar-mini" onload="onload();">
  <div class="wrapper">
   <div id="header">
   </div>
   <!-- Left side column. contains the logo and sidebar -->
   <div id="sidebar">
   </div>
   <!-- Content Wrapper. Contains page content -->
   <div id="content" class="content-wrapper clearfix">
    <!-- Content Header (Page header) -->
   </div>
   <!-- /.content-wrapper -->
   <div id="footer">
   </div>
   <!-- Add the sidebar's background. This div must be placed
  immediately after the control sidebar -->
   <div class="control-sidebar-bg"></div>
  </div>
  <!-- ./wrapper -->
  <!-- jQuery 2.2.3 -->
  <script src="../resources/plugins/jQuery/jquery-2.2.3.min.js"></script>
  <!-- Bootstrap 3.3.6 -->
  <script src="../resources/bootstrap/js/bootstrap.min.js"></script>
  <!--左側(cè)菜單-->
  <script src="../resources/dist/js/common/global.js"></script>
  <script src="../resources/dist/js/menu/menuTemplate.js"></script>
  <script src="../resources/dist/js/menu/menu.js"></script>
 </body>
<script>
 //加載頁面布局的header,sidebar,footer的內(nèi)容
 $("#header").load("inc/header.html");
 $("#sidebar").load("inc/sidebar.html");
 $("#footer").load("inc/footer.html");
 /*
 *加載變換內(nèi)容,主要url參數(shù)為dom對象,并且該dom中的url放在href中,
 *調(diào)用方式如:<span onclick="javascript:load(this);" href="/backstage/website/test.html" rel="external nofollow" rel="external nofollow" >測試</span>
 *注意:1.該dom對象最好不要用a標(biāo)簽,因為點擊a標(biāo)簽會進入href指定的頁面
 *  2.要加載的內(nèi)容要用 id="content" 標(biāo)注,因為load中指明了加載頁面中指定的id為content下的內(nèi)容
 * 3.對應(yīng)頁面的JavaScript寫在content下
 */
 function load(url, data){
  //alert($(url).attr("href"));
  $.ajaxSetup({cache: false });
  $("#content").load($(url).attr("href")+ " #content ", data, function(result){
   //alert(result);
   //將被加載頁的JavaScript加載到本頁執(zhí)行
   $result = $(result); 
   $result.find("script").appendTo('#content');
  });
 }
</script>
</html>

被加載頁面:

<div id="content">
 <div>測試二</div>
 <span onclick="javascript:load(this);" href="/backstage/website/test.html" rel="external nofollow" rel="external nofollow" >測試</span>
 <a href="javascript:test();" rel="external nofollow" >測試</a>
 <script>
  function test(){
   alert("測試二頁面");
  }
 </script>
 <script>
  function test2(){
   alert("ceshi");
  }
 </script>
</div>

效果截圖:

以上所述是小編給大家介紹的使用jQuery的load方法設(shè)計動態(tài)加載及解決被加載頁面js失效問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論