简体   繁体   中英

javascript not working in php file

There is a file index.php, in this file javascript is connected

<!DOCTYPE html>
<html lang="uk">
<head>
<meta charset="utf-8">
<title>Fiji Travel</title>
<link href="normalize.css" rel="stylesheet">
<link href="style.css" rel="stylesheet">
<link href="main/main.css" rel="stylesheet">
<link href="main/login.css" rel="stylesheet">
<link href="http://fonts.googleapis.com/css?family=Oswald" rel="stylesheet">
<link href='http://fonts.googleapis.com/css?family=Arvo' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="wrapper">
    <div class="center">
        <!-- Header -->
        <header>
            <h1>Fiji Travel</h1>
            <nav>
                <ul>
                    <li><a href="/index.php" class="active">Main</a></li>
                    <li><a href="/blog/blog.php">Blog</a></li>
                    <li><a href="/gallery/gallery.php">Gallry</a></li>
                    <li><a href="/about/about.php">About</a></li>
                    <li><a href="/contacts/contacts.php">Contacts</a></li>
                </ul>
            </nav>
            <img src="../images/img02.jpg" alt="img02" class="slide">
        </header>
        <!-- Sidebar -->
        <?php require 'modules/sidebar.php'; ?>
        <!-- Content -->
        <main class="slide">
            <p>asdasd</p>
        </main>
    </div> <!-- end center -->
    <div class="clearfix"></div>
</div> <!-- end wrapper -->
<!-- Footer -->
<?php require 'modules/footer.php'; ?>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>

I run the site through denwer. The problem is that the script in this file is not running.

$(document).ready(function() {
    $(document).on("click", "a", function(e) {
        if ( $(this).hasClass('active') ) return;
        $("a").removeClass('active');
        $(this).addClass('active');
        $(".slide").slideUp(800);
        var self = this;
        setTimeout(function() {
            window.location.href = $(self).attr("href");
        }, 800);
    });
});

When the file was index.html, everything worked. I tried to connect scripts with

<?php
echo ' <script type="text/javascript" src="script.js"></script>';
?>

, but it still doesn't work. I noticed that if the site hangs, then javascript starts working. How to solve this?

the error was in my js file. I forgot preventDefault()

$(document).ready(function() {
  $(document).on("click", "a", function(e) {
    e.preventDefault();
    if ( $(this).hasClass(`active`) ) return;
    $("a").removeClass(`active`);
    $(this).addClass(`active`);
    $(".slide").slideUp(800);
    setTimeout(() => {
      window.location.href = $(this).attr("href");
    }, 800);
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM