jQuery .load PHP into a Div -not working-

2019-09-09 04:31发布

问题:

This is a fantastic resource and I have been using it extensively during my first website developing project.

I have static webpages that I want to maintain static but introduce WordPress (already installed and running on a /blog/ subdomain) onto the static homepage within a specified div. I found the code:

<script typ="text/javascript">
    $(document).ready(function(){
         $("#randomdiv").load("load.php");
    });
</script>

Which I modified from "#randomdiv" to "#myblog" and "load.php" to the "blog/index.php" file that WordPress added. I'm also specifying that I want #content from index.php.

<script typ="text/javascript">
    $(document).ready(function(){
         $("#myblog").load("blog/index.php #content");
    });
</script>

<div id="myblog" class="grid_8">
"Loading..."
</div>

The kicker: it worked for about an hour last night. I was styling the php in the CSS for my static page and it stopped working! I have cleared everything and started over but no luck. Where am I going wrong???

--Thank you for your responses--

Maybe someone can quickly and easily explain to me how .php works. WordPress gives me about 15 .php files inside /blog/ and I assumed index.php was my ticket, but maybe not. Still, lat night this worked. I posted a new post and it appeared, I was styling it and everything. Something has shifted and I can't find it.

回答1:

WordPress was placing my index.php at http:/ / ... com/blog/index.php instead of http:/ /www ... com/blog/index.php.

I went into my WordPress dashboard, navigated to settings, and changed WordPress address (url) and site address (url) to include "www".



回答2:

Try $('#myblog').load('blog/index.php'). I you only need the #content element from the HTML loaded - consider requesting an another url that will return only the part of HTML code you need.

Also, probably, you broke your HTML when styling it, so jQuery does not find the #content element when HTML it is loaded with .load function - then it does not (and should not) show any error messages.