简体   繁体   English

提示不适用于php页面

[英]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. 我只是在这里输入提示以确保代码按预期工作,然后目标是将更多项放入if语句中,但最初无法使其工作。 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. 我已经在js文件中的任意位置放置了确认函数,但是该函数不起作用,但是如果我将它与带有脚本标签的实际index.php页面一起使用,它就可以正常工作。 I'm at a loss. 我很茫然。 This code is located in the js file. 此代码位于js文件中。

$(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. 预计到达时间:删除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. document.write将不再使用。 It's a deprecated feature of JavaScript bad practice . 是JavaScript 不良做法 不推荐使用的功能

In your case, since you are running it after the DOM has loaded, it will erase your entire page and then write "lorem" . 在您的情况下,由于您是在DOM加载运行它的,因此它将擦除您的整个页面,然后写入"lorem" Once that happens, your '#main-menu ul li a' element(s) will no longer exist. 一旦发生这种情况,您的'#main-menu ul li a'元素将不再存在。

Also, $(function(){ }); 另外, $(function(){ }); is shorthand for $(document).ready(function(){}) , so there's no reason to be using both. $(document).ready(function(){})简写,因此没有理由同时使用两者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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