繁体   English   中英

单击链接将其显示在同一页面上的不同Div中

[英]Clicking a link display it in Different Div on Same Page

我想知道是否有一种方法可以单击导航Div标签上的链接,并将其显示在内容Div上,就像我有

<div id="nav">
<a href="infoto (div id="content")">a link </a></div>
<div id="content>show the stuff</div> 

从以下评论中,OP指出以下内容:

我正在尝试重做一个网站,但我的困惑正在使我变得更好。 如果我有三个链接,例如首页,作者和我们的使命。 我希望能够单击有关author的信息,并在main_content div标签中显示html文件aboutauthor.html。

替代1:使用jquery标签:在此处查看演示和代码: http//jqueryui.com/demos/tabs/

Alt 2:在同一个html文件中隐藏/显示div:

HTML:

<div id="nav">
    <a href="#content1">Show content 1</a>
    <a href="#content2">Show content 2</a>
    <a href="#content3">Show content 3</a>
</div>

<div id="content1" class="toggle" style="display:none">show the stuff1</div> 
<div id="content2" class="toggle" style="display:none">show the stuff2</div>
<div id="content3" class="toggle" style="display:none">show the stuff3</div>

jQuery的:

$("#nav a").click(function(e){
    e.preventDefault();
    $(".toggle").hide();
    var toShow = $(this).attr('href');
    $(toShow).show();
});

演示: http : //jsfiddle.net/5NEu3/3/

替代3:从服务器按需加载:

要将html加载到您的主div中,您应该使用: http : //api.jquery.com/load/遵循该站点上的示例。 并且请注意,您正在加载的html端必须与托管新页面的域位于同一域中。

HTML

<a href="http:www.test.com/test1.html">Show content 1</a>
<a href="http:www.test.com/test2.html">Show content 2</a>

jQuery的

$("#nav a").click(function(e){
        e.preventDefault();
        $('#maindiv').load($(this).attr("href"));
});

使用jQuery,您可以执行以下操作。

这将打开网站<a href="example.html">并在您单击它时将其放在<div id="content"> ,然后禁用更改整个网站。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js" type="text/javascript"></script>
<script>
 $(document).ready(function() {
  $('#nav a').click(function(e) {
   e.preventDefault();
   $('#content').load($(this).attr('href'));
  });
 });
</script>

<div id="nav">
   <a href="somepage.html">Some page</a>
   <a href="someotherpage.html">Some other page</a>
   <a href="smypage.html">My page</a>
</div>
<div id="content">
 show the stuff
</div>  

像这样:

function setContent(div, content)
{
    getElementById(div).innerHtml = content;
    return false; // return false to ignore the click
}

<a href="path-to-non-js-browsers-and-search-engines.html" onClick="setContent('content','foo');">a link </a>

显示一个隐藏的div(我想这就是您想要的)

javascript(使用jQuery)

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
  $(document).ready(function () {
    $("#showdiv").click(function () {
       $("#hiddendiv").show();             
    });
  });
 <script>

HTML

<a href="javascript: void(0)" id="showdiv">Show the Div</a>
<div id="hiddendiv" style="display:none;">Content here</div>

你可以在这里看到它的作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM