为什么要防止默认不起作用?

时间:2016-08-22 00:57:05

标签: php html5

当点击链接时,我正试图让我的div#产品不动。 已经尝试过“preventDefault”或“return false”,它根本不起作用,页面仍然刷新或者(a)链接在点击时变得冻结(产品无法添加到数据库中)。任何人都可以告诉我如何写它?只需将其写入我的代码中即可。

<!-- This is my index.php -->

    <!doctype html>
    <?php include ("functions/functions.php"); ?>
    <html>
    <head>
    <meta charset="utf-8">
    <title>Learning</title>
    <link rel="stylesheet" href="styles/style.css" type="text/css" media="all" />
    <script src="js/jquery-3.0.0.min.js"></script>

    <script type = "text/javascript" language = "javascript">
       $(document).ready(function() {
          $("a").click(function(event){
             event.preventDefault();
             alert( "Added to cart!" );
          });
       });
    </script>

</head>

<body>
    <div id="container">
        <div id="header"> </div>

            <div id="products">
                <?php getPro(); ?>
            </div>

        <div id="footer"> </div>
    </div>
</body>

//这是我的functions.php

function getPro(){
    if(!isset($_GET['cat'])){
        if(!isset($_GET['brand'])){

    global $con;
    $get_pro = "select * from products";
    $run_pro = mysqli_query($con, $get_pro);
    while ($row_pro=mysqli_fetch_array($run_pro)){
        $pro_id = $row_pro['product_id'];
        $pro_cat = $row_pro['product_cat'];
        $pro_brand = $row_pro['product_brand'];
        $pro_title = $row_pro['product_title'];
        $pro_image = $row_pro['product_image'];

        echo "<div id='single_product'>
                    <h1>$pro_title</h1>
                    <img src='admin_area/product_images/$pro_image' width='135' height='150'/>
                    <a href='?add_cart=$pro_id'>Add2Cart</a>
                    </div>
                </div>";
    }
        }
    }
}

0 个答案:

没有答案