2016-05-02 222 views
-1

所以我有一个配置文件页面,状态更新/墙,我有限的页面上的“关于”信息,我有文字说“更多关于我..”,我想转进入一个链接,点击时,隐藏“墙”并用“关于”信息替换它。我不知道如何做到这一点,我尝试了一些JavaScript方法,但他们甚至都没有工作。更改数据而不刷新页面

我试图让这项工作几天。任何帮助将是伟大的!

+2

我认为你必须使用Ajax来取代 '墙' 的锚链接不刷新页面。 –

+0

墙壁将默认显示,我只是想要它,所以点击时,它会隐藏墙壁,并显示“更多关于”信息 –

+0

请提供你已经尝试过的代码.. –

回答

0

$("#more_about_me").on('click', function() { 
 
    $("#wall").hide(); 
 
    $("#more_about_me").hide(); 
 
    $("#about_text").show(); 
 
}); 
 
$("#less_about_me").on('click', function() { 
 
    $("#wall").show(); 
 
    $("#more_about_me").show(); 
 
    $("#about_text").hide(); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="wall"> 
 
    <h3>Wall</h3> 
 
    Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book 
 
</div> 
 
<h3>About Me</h3> 
 
Lorem Ipsum has been the industry's standard dummy text. 
 
<a href="javascript:;" id="more_about_me">More About me</a> 
 
<div id="about_text" style="display:none"> 
 
    Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has 
 
    survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing 
 
    software like Aldus PageMaker including versions of Lorem Ipsum. 
 
    <a href="javascript:;" id="less_about_me">Less About me</a> 
 
</div>

+0

这将是完美的,我可以看到它的作品,但我试着在测试页上,但它没有工作,链接什么都没有。即时通讯不太确定为什么 –

+0

我刚刚尝试在我的系统上创建测试文件,它适用于我..你可以从这里下载文件:http://wikisend.com/download/458736/test.html让我知道这个文件在你的系统中工作与否。 –

+0

这是完美的,我的问题是,我的脚本上面的代码,而不是下面的,虽然我不认为这会导致它无法运行。谢谢! –

0

你可以做一个纯js的ajax调用或更简单的jQuery ajax调用。

<div id="about"><h2>About.....</h2></div> 

<button>Load more</button> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> 
<script> 
$(document).ready(function(){ 
    $("button").click(function(){ 

    $("#about").append("<div></div>"); //append a div inside hold the content to be loaded 

    $("#about>div").load("url of file to be loaded"); // load the content into children div 

    }); 
}); 
</script> 
+0

没有加载页面替换div。不重新加载 –

+0

这几乎与我试用的几乎完全相同,在我的控制台上显示“Uncaught TypeError:$不是函数” –

+0

@ Joe Bills请确保您正在加载jQuery库。 –

0

您可以使用jQuery ajax。

HTML:

<div class="sidebar"> 
    <div class="wall"> 
     My Wall Content 
    </div> 
    <div class="about-us-more" style="display:none;"> 
     Initially hidden 
    </div> 
</div> 
<div class="main"> 
    <button type="button" id="load-more-about-us">More Info</button> 
</div> 

JQUERY AJAX

$("#load-more-about-us").click(function(){ 
    $.get("get_more_about_us.php", function(data, status){ 
     $(".wall").hide(); 
     $(".about-us-more").show(); 
    }); 
});