duanrong0738 2019-01-18 13:52
浏览 124

按div时重定向

I am trying to get it when someone presses the div that it will give an alert en after a view seconds it will redirect automatic.

My php code:

<?php
    if(!isset($_SESSION['USER'])) { ?>
        <a href="autohuren.php">
            <div class="btn btn-outline btn-default" style="background-color:white;">
                Huur deze auto.
            </div>  
        </a>
<?php } ?>

This code will redirect but not when pressing the div. And their is no alert.

if(!isset($_SESSION['USER'])) { 
echo "Please Log In First";
echo "<script>setTimeout(\"location.href = 'http://www.forobd2.com';\",1500);</script>";
} ?>

So what I want is that the div when pressed alert with a message and redirect after a few seconds. I hope someone can help me with this!

  • 写回答

1条回答 默认 最新

  • douzhun8615 2019-01-18 13:59
    关注

    You first need to remove the default event on the link. Then you need to wait 2 seconds and make a redirect.

    <a id="link" href="autohuren.php">
        <div class="btn btn-outline btn-default" style="background-color:white;">
            Huur deze auto.
        </div>  
    </a>
    
    
    <script type="text/javascript">         
    document.querySelector('[href="autohuren.php"]').addEventListener("click", function(event) {
        event.preventDefault();
        <?php if(isset($_SESSION['USER'])) { ?>
        window.setTimeout(function(){ 
            window.location.replace("autohuren.php");
        },2000);
        <?php }else{ ?>
        alert('Please Log In First');
        <?php } ?>
    }, false);
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?