为什么我的jquery脚本在我的包含php文件中不起作用?

时间:2019-02-08 09:07:48

标签: php jquery html html5

我试图了解为什么我的jQuery脚本在我的PHP包含文件中根本不起作用。 index.php:

<?PHP  
session_start();
include_once("./tools/globalFunction.php");
?>
<!doctype html>
<html lang="fr">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
        <title>Jojo Site</title>
    </head>
    <body>
        <?PHP 
        include_once("application.php");
        include_once("layout/menu.php");
        include_once("layout/loginModal.php");
        include_once("layout/container.php");
        include_once("layout/footer.php");  
        ?>
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
        <script type="text/javascript" src="./editor/ckeditor/ckeditor.js"></script>
        <script type="text/javascript" src="./js/javaScriptGlobalFunction.js"></script>
    </body>
</html>

menu.php:

    <div class="collapse navbar-collapse justify-content-end" id="navbarNavConnexion">
        <ul class="navbar-nav">    
            <li id="adminButton" class="nav-item">
                <?php
                if (!$_SESSION['admin']){?>
                <button id="connexion" class="btn btn-dark" href="#" data-toggle="modal" data-target="#loginModal">Connexion</button> <?php ;}
                else {?>
                <button id="deconnexion" class="btn btn-dark" href="#" >Déconnexion</button> <?php ;}    
                ?>
            </li>
        </ul>
    </div>

javaScriptGlobalFunction.php:

$("#Connexion").click(function(){
    alert('connexion')
});
$("#Deconnexion").click( function() {
    alert('déconnexion')
});   

使用这些代码行将不起作用,但是如果我将jquery脚本include和我的脚本放在菜单文件的末尾,如:

    <div class="collapse navbar-collapse justify-content-end" id="navbarNavConnexion">
        <ul class="navbar-nav">    
            <li id="adminButton" class="nav-item">
                <?php
                if (!$_SESSION['admin']){?>
                <button id="connexion" class="btn btn-dark" href="#" data-toggle="modal" data-target="#loginModal">Connexion</button> <?php ;}
                else {?>
                <button class="btn btn-dark" href="#" id="deconnexion">Déconnexion</button> <?php ;}    
                ?>
            </li>
        </ul>
    </div>
</nav>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script type="text/javascript">
$("#deconnexion").click(function(){alert('deconnexion')});
</script>

它正在运行...是否有我看不见的东西,或者我必须将jquery和脚本直接放在每个文件中?

2 个答案:

答案 0 :(得分:1)

它可能无法正常工作,因为在javaScriptGlobalFunction.js中,您尝试按id大写首字母查找元素,而在html中,id均为小写字母。 在javaScriptGlobalFunction.js中更改

$("#Connexion").click(function(){
    alert('connexion')
});
$("#Deconnexion").click( function() {
    alert('déconnexion')
});

$("#connexion").click(function(){
    alert('connexion');
});
$("#deconnexion").click(function() {
    alert('déconnexion');
});

答案 1 :(得分:0)

首先按korrekt下/上键: $(“#Deconnexion”)!= id =“ connexion”

您的脚本也将运行,因为您的html是由浏览器呈现的。 放入

$(function () {
  $("#connexion").click(function(){
    alert('connexion');
  });
  $("#deconnexion").click(function() {
    alert('déconnexion');
  });
});