jquery + ajax +js +tab切换

2010-06-23 13:28:44 by 【6yang】, 129 visits, 收藏 | 返回

 

<!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>v1.1 tab function</title>
<style type="text/css">
  body {font-size:12px; }
  #tab0,#tab1,#tab2 {background:#F00; cursor:hand; color:#fff;}
</style>
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
  $(document).ready(function()
  {
  //————————-
    //tabs config
    var tabs = ["#tab0", "#tab1", "#tab2"]; //tab 数组 id
    var datas = "#div_data"; //显示区对象的id号
    var event_type = "click"; //触发事件(click/dblclick/mouseover/focus/…)
    var default_tab = "#tab0";
    //切换图片
    var tab_selected_bgimg = "images/green.png";
    var tab_unselected_bgimg = "images/gray.png";
    //切换文本颜色
    var tab_selected_txtcolor = "#000";
    var tab_unselected_txtcolor = "#fff";
    //
    urls = [
          "my_ajax_server.php?app=tab0",
          "my_ajax_server.php?app=tab1",
          "my_ajax_server.php?app=tab2",
         ]

    //**Begin:固定代码*********************************
    for (var i=0; i<tabs.length; i++)
    {
      $(tabs[i]).bind(event_type, handler);
    }
    $(default_tab).trigger(event_type);
    //
    function handler()
    {
      //初始化缓存数组
      var localdatas = new Array(); //缓存浏览器本次数据
      for (var i=0; i<tabs.length; i++)
      {
        localdatas[i]="";
      }

      //重置所有tabs
      for (var i=0; i<tabs.length; i++)
      {
        $(tabs[i]).css("background-image", "url("+ tab_unselected_bgimg +")");
        $(tabs[i]).css("color", tab_unselected_txtcolor);
      }

      var curr_index;
      for(var i=0;i<tabs.length;i++)
      {
        if($(tabs[i]).attr("id")==$(this).attr("id"))
        {
          curr_index = parseInt(i);
        }
      }
      //
      $(this).css("background-image", "url("+ tab_selected_bgimg +")");
      $(this).css("color", tab_selected_txtcolor);

      if(localdatas[curr_index]=="")
      {
        //ajax获取数据(默认method=get)
        $.ajax
        ({
          url: urls[curr_index], //后台处理程序
          cache: false,
          timeout: 20000,
          error:function()
          {
            alert("error while submitting");
          },
          success:function(data)
          {
            localdatas[curr_index] = data; //缓存浏览器本次数据
            $(datas).html(data);
          }
        });
      }
      else
      { //显示缓存数据
        $(datas).html(datas[curr_index]);
      }
    }
    //**End:固定代码*********************************
  //—————–
  });

</script>
</head>

<body>
<table class="tabWrap">
<tr>
  <td width="97" id="tab0">tab0</td>
  <td width="30"></td>
  <td width="97" id="tab1">tab1</td>
  <td width="30"></td>
  <td width="97" id="tab2">tab2</td>
  <td width="149"></td>
</tr>
</table>

<table  >
<tr>
  <td>
    <div id="div_data"></div>
  </td>
</tr>
</table>

</body>
</html>

 

<?php
  /*******************************************
  * File: my_ajax_server.php
  ********************************************/

  error_reporting(7);
  set_magic_quotes_runtime(0);

  $app = $_GET["app"];

  switch ($app)
  {
    case "tab0":  //
?>
 F1
<?php
      break;

    case "tab1":  //
?>
 F2
<?php
      break;

    case "tab2": 
?>
 F3
<?php
      break;

    default:
      echo "my_ajax_server.php error.";
      break;
  }
?>

分享到:
share

    图片原图

    loading

    loading