简体   繁体   English

从隐藏的Div加载内容?

[英]Load Content From a Hidden Div?

There is a tutorial here: http://perishablepress.com/slide-fade-content/ 这里有一个教程: http//perishablepress.com/slide-fade-content/

and the code it provides is: 它提供的代码是:

$(document).ready(function(){
    // Ajax Slide & Fade Content with jQuery @ http://perishablepress.com/slide-fade-content/
    $('.more').live('click',function(){
        var href = $(this).attr('href');
        if ($('#ajax').is(':visible')) {
            $('#ajax').css('display','block').animate({height:'1px'}).empty();
        }
        $('#ajax').css('display','block').animate({height:'200px'},function(){
            $('#ajax').html('<img class="loader" src="http://example.com/slide-fade-content/loader.gif" alt="">');
            $('#ajax').load('http://example.com/slide-fade-content/slide-fade-content.html.html #'+href,function(){
                $('#ajax').hide().fadeIn().highlightFade({color:'rgb(253,253,175)'});
            });
        });
        return true;
    });
});

This will load content from an external file, is there a way to do something similar but to load the content from a hidden div on the same page? 这将从外部文件中加载内容,是否可以执行类似的操作,但要从同一页面上的隐藏div加载内容?

replace 更换

$('#ajax').load('http://example.com/slide-fade-content/slide-fade-content.html.html #'+href,function(){

with

var contentTobeLoaded=$("#myHiddenDivId").html()
$('#ajax').html(contentTobeLoaded).fadeIn(600,function(){

Assuming you have the hidden div with the id myHiddenDivId 假设你有一个id为myHiddenDivId的隐藏div

EDIT : As from your comment and sample link provided, Here is my updated solution 编辑:从您提供的评论和示例链接,这是我更新的解决方案

1) Have the content for each item in a seperate div and hide it. 1)将每个项目的内容放在一个单独的div中并隐藏它。 this div should have unique id 2) when you click on the links you get the id and load content from the hidden div corresponding to that. 这个div应该有唯一的id 2)当你点击链接时你得到id并从隐藏的div中加载内容。

HTML HTML

  <div id="divHiddenContainer" style="display:none;">
      <div id="divItem1"><span style="background-color:Gray;">God, My description about item 1 goes here</span></div>
      <div id="divItem2"><span style="background-color:yellow;">Brother,My description about item 222 goes here</span></div>
      <div id="divItem3"><span style="background-color:orange;">Hello,My description about item 333 goes here</span></div>
  </div>

   <a href="#" class="aItemLnk" id="a1">Item 1</a>
   <a href="#" class="aItemLnk" id="a2">Item 1</a>
   <a href="#" class="aItemLnk" id="a3">Item 1</a>

   <h3>Content goes here</h3>
    <div id="ajax"></div>

Javascript 使用Javascript

$(".aItemLnk").click(function () {

    var id = $(this).attr("id").replace(/^.(\s+)?/, "");
    var contentTobeLoaded = $("#divItem" + id).html();          

    $('#ajax').html(contentTobeLoaded).fadeIn(600, function () {
      //do whatever you want after fadeIn
    });
});

Here is the working sample : http://jsfiddle.net/9xZrq/ 这是工作示例: http : //jsfiddle.net/9xZrq/

Second sample which has the fadeOut before fadeIn : http://jsfiddle.net/9xZrq/1/ fadeO之前有fadeOut的第二个样本: http//jsfiddle.net/9xZrq/1/

You can change the delay you need for fadeIn from 600 to 1200 or 1500 or whatever you want 您可以将fadeIn所需的延迟从600更改为1200或1500或任何您想要的延迟

Note that you need to have some connection between the link id and hidden div id so that you can figure out which div to be showed. 请注意,您需要在链接ID和隐藏的div id之间建立一些连接,以便您可以确定要显示的div。

I suppose that your div already contains his data and you just want to show it, so you can use: 我想你的div已经包含了他的数据而你只想展示它,所以你可以使用:

$('#id_of_your_div').show().fadeIn();

Or I've mistaken you and you want to load the content from a div to an another one? 或者我误认为你想要将div的内容加载到另一个中? So you can retrieve his content with html() . 所以你可以用html()检索他的内容。

If I understand right... just call your object html attribute to do that... 如果我理解正确,只需调用您的对象html属性即可...

$('#yourdiv').html();

This will return the content of the div no matter it is hidden or no. 无论div是隐藏的还是否,这都将返回div的内容。

应该可以做这样的事情(如果我正确地收集你) - http://jsfiddle.net/HTrep/6/

You should be able to load content from a hidden div fairly simply, since you're using jquery to have the $() method. 您应该能够相当简单地从隐藏的div加载内容,因为您正在使用jquery来具有$()方法。

Give the div an id, then using $('#id-of-element').innerHTML will give you the contents of the hidden div. 给div一个id,然后使用$('#id-of-element').innerHTML将为你提供隐藏div的内容。

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

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