使用AJAX在div中加载下拉菜单内容

时间:2015-02-20 23:08:31

标签: javascript jquery html ajax twitter-bootstrap

我的目标是点击下拉菜单(div id = cssmenu)元素并在使用AJAX中显示数据,所以我制作了一个小的js代码,但我不知道如何让它加载到div我提到并删除了div之前显示的内容。

HTML

<body>

<div class="container">
  <div class="row"> 
  <div class="col-md-3" >

     <div id='cssmenu'>
<ul>
   <li class='active'><a href='#' data-url="http://localhost/bioinformatica/Main_page/Quick_search.html#qhelp"><span>Home</span></a></li>
   <li class='has-sub'><a href='#'><span>About</span></a>
      <ul>
         <li><a href='#'><span>Project</span></a></li>
         <li class='last'><a href='#'><span>Team</span></a></li>
      </ul>
   </li>
   <li class='last'><a href='#'><span>News</span></a></li>
</ul>
</div> 
    </div>

    <div class="col-md-9">
    <div id="tabs">
    <ul class="nav nav-tabs" id="prodTabs">
        <li class="active"><a class="clickableLink" href="#tab_quick" data-url="http://localhost/bioinformatica/Main_page/Quick_search.html#qhelp">Quick Search</a></li>
        <li><a class="clickableLink" href="#tab_advanc" data-url="#">Advanced Search</a></li>
        <li><a class="clickableLink" href="#tab_struct" data-url="something3.txt">Structure Search</a></li>
    </ul>
    <div class="tab-content">
        <div id="tab_quick" class="tab-pane active"></div>
        <div id="tab_advanc" class="tab-pane active"></div>
        <div id="tab_struct" class="tab-pane active"></div>
    </div>
</div>

  </div>
    </div>


    <div class="clearfix visible-lg"></div>
  </div>
</div>

</body>
</html>

的Javascript

$( document ).ready(function() {

$('#cssmenu a').click(function (e) {
    e.preventDefault();

    var url = $(this).attr("data-url");
    var href = this.hash; 
    var pane = $(this);

    // ajax load from data-url
    $(href).load(url,function(result){      
        pane.tab('show');
    });

});
});

例如在菜单中,当我点击Home时,我希望它在div id = tabs中加载它的data-url attr,即用Home data-url替换所有内容。谢谢 !

1 个答案:

答案 0 :(得分:1)

您可以尝试以下操作:

$( document ).ready(function() {    
    $('#cssmenu a').click(function (e) {
        e.preventDefault();

        var url = $(this).data('url'); // this is the correct syntax
        var pane = $(this);

        $('#tabs').load(url, function(result){ // load the content directly to #tabs     
            pane.tab('show'); // display the tab
        });   
    });
});