【问题标题】:Display value of the database when hovered悬停时显示数据库的值
【发布时间】:2013-12-31 19:44:52
【问题描述】:

我想为我的网站制作一个活动日历。在日历上,我想在悬停时在某个日期显示整个 event_description。但我不知道怎么做。我使用 jquery 和 mysqli 进行悬停和检索数据。

这是我当前项目的图像 http://i.stack.imgur.com/xNgZM.jpg

我有一个包含“event_id、event_title、event_date 和 event_description”的数据库

这就是我从数据库中获取价值的方式。

    while($r=$q->fetch_assoc()) {
        $event_id=$r['event_id']; //This is a unique ID from my DB
        $event_title=$r['event_title']; //Obvious title is obvious
        $event_desc=$r['event_desc']; // I want to post this on the popbox at the bottom
        echo $event_title."<br />";
    <a class='popper' data-popbox='pop2' href='viewEvent?id=$event_id'>See more</a><br />";
    echo "<div id='pop2' class='popbox'>";
    echo " <h2>Event for today!!</h2>";
    echo " This text should have the 'event_description'";
    echo "</div>";

div ID pop2 和 class popbox 是链接悬停时的代码。这是脚本

    $(function() {
    var moveLeft = 0;
    var moveDown = 0;
    $('a.popper').hover(function(e) {
    var target = '#' + ($(this).attr('data-popbox'));
    $(target).show();
    moveLeft = $(this).outerWidth();
    moveDown = ($(target).outerHeight() / 2);
    }, function() {
    var target = '#' + ($(this).attr('data-popbox'));
    $(target).hide();
    });
    $('a.popper').mousemove(function(e) {
    var target = '#' + ($(this).attr('data-popbox'));
    leftD = e.pageX + parseInt(moveLeft);
    maxRight = leftD + $(target).outerWidth();
    windowLeft = $(window).width() - 40;
    windowRight = 0;
    maxLeft = e.pageX - (parseInt(moveLeft) + $(target).outerWidth() + 20);
    if(maxRight > windowLeft && maxLeft > windowRight)
    {
    leftD = maxLeft;
    }
    topD = e.pageY - parseInt(moveDown);
    maxBottom = parseInt(e.pageY + parseInt(moveDown) + 20);
    windowBottom = parseInt(parseInt($(document).scrollTop()) +                parseInt($(window).height()));
    maxTop = topD;
    windowTop = parseInt($(document).scrollTop());
    if(maxBottom > windowBottom)
    {
    topD = windowBottom - $(target).outerHeight() - 20;
    } else if(maxTop < windowTop){
    topD = windowTop + 20;
    }
    $(target).css('top', topD).css('left', leftD);
    });
    });

请告诉我你希望看到什么代码来帮助我解决这个问题。在此先感谢 ;)

【问题讨论】:

  • 一种简单的方法是在悬停时调用 ajax 并在工具提示中显示检索到的描述,如果事件不是太多,另一种方法是将所有事件按日期存储在 javascript 数组中并在悬停时, 按日期过滤数组
  • 嗨! Joachim,我访问过该站点,并且有很多工具提示!我认为我的解决方案将是 AJAX 对吗?但我对 AJAX 调用和与之相关的事情一无所知。
  • 嗨米纳斯!这实际上是我的第一选择!我很想使用这种数组方法,但我想我必须假设将有大量事件存储在这个数组中。你觉得数组调用可以处理吗?
  • 对于数组方法,仅在日历中加载当前月份的事件,当日历上的月份发生变化时,您可以调用ajax在数组中加载该月的事件

标签: php jquery hover


【解决方案1】:

您可以按照以下步骤操作

(1)。你应该有一个像下面这样的悬停事件

例如。

$(".popper").hover(function(){
//.load should be here
$('#pop2').fadeIn(800);},function(){$("#pop2").fadeOut(800);});

示例http://jsfiddle.net/7dcuh/21/

(2)。现在你需要做的是,使用 .load 显示数据块

例子

$( "#pop2" ).load( "getevent.php", { eventid: <?php echo $event_id; ?> }, function() {alert( "done" );});

将 eventid 作为参数传递给 getevent.php。

阅读更多http://api.jquery.com/load/

对不起,我的英语不好。

【讨论】:

  • 我将鼠标悬停在“链接”上,但问题是我无法将数据库中的数据传递到悬停框上的消息中。
  • 我已经编辑了上面的 jsfiddle 请检查一下。 link
  • hmm.. 我现在确实有悬停,我添加的负载什么也没做。我真的被难住了:(
  • 是的~!我已经能够在我的两个链接上显示一个内容。我只需要在其他链接上这样做! pop2 在 while 循环中,只要有一个事件,就会有很多 pop2。所以我需要根据他们的日期显示他们相应的描述!我们很接近了!非常感谢:D
  • 一个id只能在一个文档上使用一次。多个pop2 id不应该存在于一个页面上...
猜你喜欢
  • 2015-12-11
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
  • 1970-01-01
  • 2017-12-29
  • 1970-01-01
  • 2012-06-22
相关资源
最近更新 更多