1

我不是程序员,我主要是设计师,所以我在开发某些东西时遇到了一些困难。

我对如何在以下场景中使用深度链接和 asual jquery(或其他更合适的插件)有疑问。

导航结构更像是:

<ul>
<li><a href="/pt/empresa/historia" title="História">História</a></li>
<li><a href="/pt/empresa/historia/missao" title="Missão">Missão</a></li>
<li><a href="/pt/empresa/historia/universo" title="Universo">Universo</a></li>
<li><a href="/pt/empresa/historia/numeros" title="números">números</a></li>
</ul>

和 div

<div id="content-mask">
<div id="content-container">
</div>
</div>

我想将单击链接页面中内容容器的内容加载到内容容器中。

4

4 回答 4

2

首先将id标签添加到ul

<ul id="myList">

然后:

$("ul#myList li a").click(function(){
   $.ajax({
      url : $(this).attr("href"),
      success:function(data){
         $("#content-container").html($(data).find("#content-container").html());
      }
   })
  return false;
})
于 2012-08-28T12:18:08.660 回答
2

您可以使用以下load方法:

从服务器加载数据并将返回的 HTML 放入匹配的元素中。

$(document).ready(function(){
    $('ul li a').click(function(e){
       e.preventDefault() // prevents the default action of the click event
       $('.content-container').load(this.href) // loads the content based on the value of href attribute 
       // or $('.content-container').load(this.href + " .content-container") in case that I have misunderstood your question
    })
})

请注意,您应该只有一个具有 class 的元素,content-container否则您将有重复的内容。

于 2012-08-28T12:18:36.490 回答
0

Edited:

As OP Stated in his comment below:

how can I make it deeplink? So that the if I enter in the addressbar, for instance, www.site.com/pt/empresa/historia, it would load the content in the div of the associated file?

Solution:

$("ul li a").click(function(e){           

    // Include your domain address in a URL string with anchor's href
    var url = "http://www.your-website-address.com/" + $(this).attr('href')

    $('#content-container').html("Loading Please Wait");
    $('#content-container').load(url);    
    e.preventDefault();

});

Additional Notes:

Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.

A WORKING DEMO

OLD:

Use jQuery .load() function to achieve this.

$("ul li a").click(function(e){           
    $('#content-container').html("Loading Please Wait");
    $('#content-container').load(this.href);    
    e.preventDefault(); 
})​;

WORKING DEMO

于 2012-08-28T12:28:15.647 回答
0

你有没有想过像这样使用 span 标签:(如果需要,你也可以使用 div 标签,同样的代码。

<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:blue; }
  span { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div>
    The values stored were 
    <span></span>
    and
    <span></span>
  </div>
<script>var div = $("div")[0];
    jQuery.data(div, "test", { first: 16, last: "pizza!" });
    $("span:first").text(jQuery.data(div, "test").first);
    $("span:last").text(jQuery.data(div, "test").last);</script>

</body>
</html>
于 2012-08-28T12:19:52.920 回答