实现用Ajax 控制右侧内容并修改了url地址,传递的时候哪里错了。麻烦大神看看

CHIHPENGCHIU 2017-04-10 02:02:51
小弟是想用Ajax去实现点击左侧菜单的时候,右边内容局部刷新,并修改url地址。(不是要直接跳转)
传递的时候貌似遗漏什么,麻烦大神们看看。谢谢

<div id="content" class="content">
<ul id="list" style="float:left;list-style:none;">
<li><a href="#beijing">北京</a></li>
<li><a href="#shanghai">上海</a></li>
<li><a href="#shenzhen">深圳</a></li>
<li><a href="#guangzhou">广州</a></li>
<li><a href="#tianjin">天津</a></li>
</ul>
</div>
<div id="content-main" style="margin-left:50px;float:left;width:820px;border:1px solid #ccc;height:520px;color:#ff3300;"></div>
...全文
131 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
jio可 2017-04-10
  • 打赏
  • 举报
回复
传了参数但是你没有使用。。。
Go 旅城通票 2017-04-10
  • 打赏
  • 举报
回复
你_loadContent都没用到对应的参数啊,都定死了。。
    function _loadContent(url){
        return $.ajax({
            url :url,//////////// "atticle01.html",
            type:"post",
            tyepdata:"html"
        }) ;
    } ;
CHIHPENGCHIU 2017-04-10
  • 打赏
  • 举报
回复
一楼是html代码,二楼是JS代码,需要引入jquery。
CHIHPENGCHIU 2017-04-10
  • 打赏
  • 举报
回复
<script>
    $(function(){
        _init();   
    }) ;
    var _history = [] ; // 记录hash的活动数据容器
    function _init(){
        var root = $("#list") ; /
        var defaultHash = root.find("li a").eq(1).attr("href") ;    
        var currentHash = window.location.hash ;        
        //
        _addToHistory(defaultHash,true) ;
        if(currentHash && currentHash != defaultHash){
            _showContent((currentHash.split("#")[1])) ;    
        }
        else{
            _showContent((defaultHash.split("#")[1])) ;
        }
        $("#list").on("click","a",function(e){
            var action = ($(this).attr("href").split("#")[1]) ;     
            _showContent(action) ;
            e.preventDefault() ;
        }) ;
        window.addEventListener("popstate",function(e){
            if(e.state && e.state.hash){
                var hash = e.state.hash ;
                if(_history[1] && hash === _history[1].hash){//存在历史记录,证明是后退事件
                    _showContent(hash.split("#")[1].toLowerCase()) ;
                }else{ // 其它认为是非法后退或者前进
                    return ;
                }
            }
            else{
                return ;
            }
        },false) ;
    } ;
    function _showContent(action){
        var samePage = _history[0]["hash"] == "#" + action ;
        if(samePage){ // 同页面,则不重新加载
            return ;
        }
        _loadContent(action + ".data").done(function(data){         
            _renderContent(data["content"]) ;           
            _addToHistory("#" + action,samePage) ;
        }).fail(function(){
            throw new Error("load content error !") ;
        }) ;
    } ;
    function _loadContent(url){
        return $.ajax({
            url : "atticle01.html",
            type:"post",
            tyepdata:"html"
        }) ;
    } ;
    function _renderContent(text){
        $("#content-main").html(text) ;     
    } ;

    function _addToHistory(hash,noState){
        var obj = {
            hash : hash
        } ;
        if(noState){
            _history.shift(obj) ;
            window.history.replaceState(obj,"",hash) ;//
        }
        else{
            window.history.pushState(obj,"",hash) ;
        }
        _history.unshift(obj) ;         
    } ;
</script>

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧