如何将id传递给文件

时间:2017-07-03 16:22:28

标签: javascript jquery tooltipster

当我将主页_game' -div悬停时,我想将ID(' data-gameid')传递给名为game_get_info.php的文件。

请问如何通过Javascript或jQuery实现这一目标? TIA

这是php代码:

<?php echo '
<div class="homepage_game home_tooltip" data-gameid="'.$game_cat_content['id'].'">
<div class="home_game_image">
    <a href="'.$game_cat_content['url'].'">
        <img class="home_thumb" src="'.$game_cat_content['image_url'].'" height="85" width="125" alt="'.$game_cat_content['name'].'"/>
    </a>
</div>
</div>';
?>

这是JS / jQuery代码:

<script type="text/javascript">
$(document).ready(function() {
$('.tooltipstered').tooltipster('destroy');
$('.home_tooltip').tooltipster({
    trigger: 'hover',
    animation: 'fade',
    animationDuration: 250,
    delay: 1000,
    onlyOne: true,
    position: 'top',
    contentAsHTML: true,
    interactive: true,
    theme: ['tooltipster-noir', 'tooltipster-noir-customized'],

    content: 'Loading...',

    functionBefore: function(instance, helper) {

        var $origin = $(helper.origin);

        if ($origin.data('loaded') !== true) {

            $.post('<?php echo $setting['template_url']; ?>/sections/ajax/game_get_info.php, function(data) {

                instance.content(data);

            });
        }
    }
});
});
</script>

1 个答案:

答案 0 :(得分:2)

试试这个:

<script type="text/javascript">
$(document).ready(function() {
$('.tooltipstered').tooltipster('destroy');
$('.home_tooltip').tooltipster({
    trigger: 'hover',
    animation: 'fade',
    animationDuration: 250,
    delay: 1000,
    onlyOne: true,
    position: 'top',
    contentAsHTML: true,
    interactive: true,
    theme: ['tooltipster-noir', 'tooltipster-noir-customized'],

    content: 'Loading...',

    functionBefore: function(instance, helper) {

        var $origin = $(helper.origin);

        if ($origin.data('loaded') !== true) {

            var itemId = $origin.attr('data-gameid');

            jQuery.ajax({
                          type: "POST",
                          url: "<?php echo $setting['template_url']; ?>/sections/ajax/game_get_info.php",
                          data: { 
                            itemId: itemId
                          },

                     beforeSend: function(){    

                          },

                     success: function(data){

                        instance.content(data);

                     }

                 });

        }
    }
});
});
</script>