【问题标题】:I'm not sure that my ajax navigation works fine我不确定我的 ajax 导航是否正常
【发布时间】:2013-04-18 07:58:23
【问题描述】:

我尝试使用历史 API 制作菜单。但在我看来,它的效果并不好,我有理由这么认为:

1 我的“头衔”没有改变

2 如果我在链接上点击快速,我会在页面重新加载时闪烁,但它不应该!(但如果我点击慢速也没关系。如何防止重新加载或闪烁?)

请告诉我我做错了什么

  $(document).ready(function(){

 sliderStartUp()




//menu click
 $('.navigation').click(function(){

//fill the storage
var storage = {
 url: $(this).children('a').attr('href'),
 title: $(this).children('a').attr('title')
 };

 history.pushState(null, storage.title, storage.url );



    $.ajax({
        url: $(this).children('a').prop("href"),
        cache: false,
        success: function(data){ //if the data you return is a css notation      (eg: #id) of the location of the title name
   JqObj = $(data);
   var addSmth = JqObj.find('#central').html();
  $("#central").html('').html(addSmth);
sliderStartUp()

  }
   });
 return false;

  });

function sliderStartUp(){
  $('#slider').nivoSlider({
effect: 'fold',
animSpeed: '500',
pauseTime: '4000'
});

 }




 });

这是我的生活

 <ul id="navig">
 <?php
if ($_SERVER['REQUEST_URI'] == "/") 
{
    $_SERVER['REQUEST_URI'] = "/index";
}
$arrayNav = array(  array (
                        'name' => 'yyyyyy',
                        'title' => 'yyyyyyyyyyy',
                        'link' =>'http://bistro.loc/index',
                        'identificator' => 'id_index',
                        'pattern' => '/index/i'
                            ),
                    array (
                        'name' => 'xxxxxx',
                        'title' => 'xxxxxxx',
                        'link' => http://bistro.loc/price',
                        'identificator' => 'id_price',
                        'pattern' => '/price/i'
                            ),
                    array (
                        'name' => 'aaa',
                        'title' => '',
                        'link' => 'http://bistro.loc/#' ,   
                        'identificator' => 'id_serv',
                        'pattern' => '/#/i'
                            ),
                    array (
                        'name' => 'ssss',
                        'title' => '',
                        'link' => 'http://bistro.loc/#' ,   
                        'identificator' => 'id_cont',
                        'pattern' => '/#/i'
                            ),
                    array (
                        'name' => 'dddd',
                        'title' => '',
                        'link' => 'http://bistro.loc/#' ,   
                        'identificator' => 'id_about',                          
                        'pattern' => '/#/i'                     
                            ));
 foreach ($arrayNav as $item)
{if (preg_match($item['pattern'],"http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] ))
    {
        echo "<li  class = \"navigation\" id = \"".$item['identificator']."\"                                                                tltle=\"".$item['title']."\"><a href=\"".$item['link']."\" class=\"active\">".$item['name']."</a></li>";
        }else{
            echo "<li  class = \"navigation\" id = \"".$item['identificator']."\" tltle=\"".$item['title']."\"><a href=\"".$item['link']."\">".$item['name']."</a></li>";}
   ;}

   ?>           
 </ul>

【问题讨论】:

  • ajax函数获取什么数据?
  • 只是一个 [div #central]。只有html。
  • 我的意思是 url: $(this).children('a').prop("href") 返回什么?
  • 现在当然可以在您的帮助下正常工作。但我有一个时刻,我首先尝试自己做。非常感谢(我不能投票,因为我的代表
  • 我可以帮助你,但我没有看到你所有的代码(我没有看到你的 javascript 返回),我建议考虑简单可用的功能(接口),如:goBackInHistory()、@987654325 @ 等。这将使您的代码更易于阅读、理解和调试;)。

标签: javascript jquery ajax history


【解决方案1】:

你的成功函数看起来不太好:

success: function(data){
   jqObj = $(data);
   var addSmth = jqObj.find('#central');
   $("#central").html('').html(addSmth);

我认为你的意思是以下?

success: function(data){ //data is normally just a string
    $("#title").html(data));

function(data){ //if the data you return is a css notation (eg: #id) of the location of the title name
  JqObj = $(data);
  var addSmth = jqObj.find('#central').html();
  $("#central").html(addSmth);

@减速:

$('li[id^=id_]') 可能会减慢速度。我建议你为此使用类。

您也可以在创建点击事件后创建nivoSlider。如果你想确保点击事件都尽快完成。

【讨论】:

  • 我按照你的建议重新编写了代码,但它仍然会随着快速点击而闪烁(每秒一次,实际上并没有那么快)
猜你喜欢
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多