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

标签 php jquery html

我试图理解为什么我的 jquery 脚本在我的 php 包含文件中根本不起作用。 索引.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>

菜单.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 脚本包含和我的脚本放在菜单文件的末尾,例如:

    <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 和脚本?

最佳答案

它可能不起作用,因为在 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');
});

关于php - 为什么我的 jquery 脚本在我的 include php 文件中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54589027/

相关文章:

html - clearfix 不适用于绝对位置

javascript - 上传时防止空闲状态(iOS 上的网络应用程序)

php - 如何改进我的附加程序功能的 if else 语句

jquery - 在行上均匀拉伸(stretch) div

javascript - Rails history.pushState 通过 AJAX 显示 URL

html - ffmpeg - 将 rtsp 流流式传输到网页

php - 页面加载时如何从 codeigniter Controller 调用方法?

php - 检索帖子数组值

javascript - SetInterval 说函数不存在

html - 如何防止我的水平菜单破坏我的网站布局?