简体   繁体   中英

Prompt won't work with php page

I've tried so many different variations and still can't get it to work. I'm just putting the prompt in there to make sure that the code is working as intended, and then the goal was to put more items into the if statement, but I can't get it to work initially. I've put the confirm function everywhere I can in the the js file and it doesn't work, but then if I put it on the actual index.php page with script tags it works just fine. I'm at a loss. This code is located in the js file.

$(document).ready(function() {

$(function (){


    $('#main-menu ul li a').each(function(){
        var path = window.location.href;
        var current = path.substring(path.lastIndexOf('/')+1);
        var url = $(this).attr('href');

        if(url == current){
            $(this).addClass('active');
            confirm(url);
        };


    });         
});

});

ETA: removed the document.write. that was just another test. here's the markup on the page

<div id="main-menu"><ul>
           <li id="home-link"><a href="index.php">Home</a></li>
           <li><a href="about.php">About</a></li>
           <li><a href="process.php">Process</a></li>
           <li><a href="portfolio.php">Portfolio</a></li>
           <li><a href="interiordesignnews.php">Press</a></li>
           <li><a href="http://domain.com/blog/" target="_blank">Blog</a></li>
           <li><a href="contact.php">Contact</a></li>
       </div></center>

document.write should no longer be used. It's a deprecated feature of JavaScript bad practice .

In your case, since you are running it after the DOM has loaded, it will erase your entire page and then write "lorem" . Once that happens, your '#main-menu ul li a' element(s) will no longer exist.

Also, $(function(){ }); is shorthand for $(document).ready(function(){}) , so there's no reason to be using both.

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