加载不同div中左侧导航的链接内容

时间:2012-08-18 20:50:38

标签: javascript jquery css html xhtml

我有顶级导航,左侧导航,其链接由点击顶部导航链接驱动,页面本身是两列布局,左侧导航和内容容器带顶部导航

 <div id="topnav">Topic1 | Topic 2 | Topic 3</div>

    <div id="navigation">
      <!--all links from topic1 by default, or topic 2 links if topic 2 clicked-->
        <a href="home.html">Home</a>
        <a href="pictures.html">Picture</a>
    </div>
    <div id="content">
         <!-- content will load here -->
    </div>

当我点击主题1时,属于主题1的所有链接都应加载到左侧导航和 当我点击图片时,我的图片应加载到#content中。 我如何在jquery或javascript中执行此操作? 请帮助......这里的新手..

2 个答案:

答案 0 :(得分:0)

HTML

<a class="loader" href="home.html">Link 1</a>
<div id="content"></div>

JavaScript(使用jQuery)

$('a.loader').on('click', function(e) {
    e.preventDefault();
    $('#content').load($(this).attr('href'));
});

就像@Musa说的那样,您加载的网址应该在您自己的域中。

答案 1 :(得分:0)

您可以使用框架:

的index.htm:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ca" lang="ca">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Site</title>
</head>
<frameset rows="100,*">
  <frame src="topnav.htm" name="topnav" />
  <frameset cols="300,*">
    <frame name="navigation" />
    <frame name="content" />
  </frameset>
</frameset>
</html>

topnav.htm:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Topnav</title>
</script>
</head>
<body>
<a href="topic1.htm" target="navigation">Topic 1</a> | 
<a href="topic2.htm" target="navigation">Topic 2</a> | 
<a href="topic3.htm" target="navigation">Topic 3</a> | 
</body>
</html>

topic1.htm:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Topic1</title>
</script>
</head>
<body>
<a href="content1-1.htm" target="content">Content 1</a> | 
<a href="content1-2.htm" target="content">Content 2</a> | 
<a href="content1-3.htm" target="content">Content 3</a> | 
</body>
</html>

然后,创建

  • content1-1.htm
  • content1-2.htm
  • content1-3.htm

对topic2.htm和topic3.htm

执行相同的操作