简体   繁体   中英

Expandable DIV on-click - on demand

I've been searching lots and found many tutorials for Javascript / jquery on click expandable divs..

eg http://www.dustindiaz.com/basement/block-toggle.html

However what I am looking for,is slightly different .. I need to load the div content on click... rather than hidden loading the div in the background... with display:none for example.

Here is a working example of content loaded on demand http://www.dynamicdrive.com/dynamicindex17/ajaxtabscontent/index.htm

Now obviously this is quite complex for such a little task.. I simply need a simple text link that expands the div below.

I hope that made sense... any ideas guys?

<script type="text/javascript">
    $(document).ready(function () {
        $("#create-content").click (function () {
            //TODO: Show some kind of "loading" indicator
            $.get("content.html", function (data) {
                //TODO: Hide the "loading" indicator
                $("#target-content").html (data);
            });
        });
    });
</script>

<a href="#" id="create-content">Click</a>
<div id="target-content></div>

This snipped will load content.html using AJAX and the respose will be used as the html content of the target div, content.html could be any resource or script on your server, BUT YOU SHOULD BE VERY CAREFULL TO AVOID XSS AND ANY OTHER SECURITY RISK.

Besides, it would be nice if you add some kind of animation to indicate that the content is loagind, otherwise the user could think that nothing is happening.

Another thing that is missing is showing some kind of error message if the request to the server fails for some reason, the documentation about $.get method can be found here: http://api.jquery.com/jQuery.get/

Here is a working example of doing what I think you want:

http://jsfiddle.net/yVbYQ/47/

$('.click').click(function(){
  $.ajax({
      type: 'POST',
    url: '/echo/html/',
      data: { html: "<p class='loaded'>hello ajax!<br/>hello ajax!<br/>hello ajax!<br/>hello ajax!</p>"},
    success: function(data) {
      $('#post').html(data);
        $('.loaded').slideDown();
    }
  });
});

and the CSS

.loaded
{
    background-color: red;
    display: none;
}

This makes the content get loaded with display: none, and then it slides down the new content making it look all stylish.

I've done this often by creating divs underneath in a RowDataBound event for Gridviews. If you're looking to not create those underneath... I suggest simply creating a function that accepts the current element, grab the parent elements until you've grabbed the parent that has the rows within it, then call your ajax:

Keep in mind, this is pseudo-code:

<script type="text/javascript">
    $(document).ready(function () {
        $("#clickable-div").click (function () {

            $.ajax({
                url: "test.html?action=getLink", 
                success: function(data){
                  $(this).parentElement.append(data);
            }});

            $(this).show("slide", { direction: "down" }, 1000);
        });
    });
</script>

Something along those lines, although that isn't working code. The data returned could be in the form of HTML... or you could JSON it and parse it within the success function and append.

如果您正在寻找极致的简单性(使用jQuery),则可以使用快捷方式:

$('#your_target_div').load('your_url');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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