简体   繁体   English

jQuery使用Ajax使用数据属性填充div

[英]jQuery populate a div with data-attributes using Ajax

I have the following (simplified) html code: 我有以下(简化)的html代码:

<div class="column span-4">
    <img src="image1">
    <h2>Title 1</h2>
    <p>Excerpt 1</p>
    <a href="#" class="button package_info" data-title="Title 1" data-img="image1" data-excerpt="Excerpt 1" data-vid="video_url_1">More Info</a>
</div>

<div class="column span-4">
    <img src="image2">
    <h2>Title 2</h2>
    <p>Excerpt 2</p>
    <a href="#" class="button package_info" data-title="Title 2" data-img="image2" data-excerpt="Excerpt 2" data-vid="video_url_2">More Info</a>
</div>

    <div class="column span-4">
    <img src="image1">
    <h2>Title 3</h2>
    <p>Excerpt 3</p>
    <a href="#" class="button package_info" data-title="Title 3" data-img="image3" data-excerpt="Excerpt 3" data-vid="video_url_3">More Info</a>
</div>

<div class="package_overview" style="width:100%;height:100%;top:100%;background:#ffffff;position:fixed;padding:35px;z-index:9999;transition:0.15s ease-out all;">
    <div class="package_overview_close"><i class="fa fa-close"></i></div>
</div>

The idea here is that when a user clicks on "More info", the div "package_overview" shows, and in that div is the information contained within data attributes on the link they clicked (ie data-title, data-excerpt, data-img, data-vid etc - there are more in my actual code, but i'm keeping this simple). 这里的想法是,当用户单击“更多信息”时,显示div“ package_overview”,并且该div中包含了他们单击的链接的数据属性中包含的信息(例如,数据标题,数据摘录,数据img,data-vid等-我的实际代码中还有更多内容,但我保持简单。

Here is my current jQuery, 这是我当前的jQuery,

jQuery(function($){
        $('a.package_info').click(function( event ) {
        event.preventDefault();
        $('.package_overview').css('top','0');
    });
    $('.package_overview_close').click(function( event ) {
        $('.package_overview').css('top','100%');
    });

Obviously, this just opens and closes the div, but how can I populate it with the data attributes when the user clicks on it? 显然,这只是打开和关闭div,但是当用户单击它时如何用数据属性填充它呢?

Use prop to get attributes of anchor link clicked and populate the html inside using html() function. 使用prop获取单击的锚链接的属性,并使用html()函数填充内部的html。

$('a.package_info').click(function( event ) {
        event.preventDefault();
        $('.package_overview').css('top','0');
        var title = $(this).prop('data-title');
        var excerpt= $(this).prop('data-excerpt'); 
        .
        .
        $('.package_overview').html('Title : '+ title + '<br/>' + 'Excerpt : ' + excerpt);

  });

you can use data() method for manage data-* attributes 您可以使用data()方法来管理data-*属性

$('a.package_info').click(function( e ) {
        e.preventDefault();
        $('.package_overview').css('top','0');
        var title = $(this).data('title');
        var excerpt= $(this).data('excerpt'); 
        $('.package_overview').html('Title : '+ title + '<br/>' + 'Excerpt : ' + excerpt);
        return false;
});

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

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