jQuery .load() 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15107074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:23:06  来源:igfitidea点击:

jQuery .load() not working

jqueryurlhtmldnsload

提问by TW_

On my website, I'm trying to pull the content of a post in my forum (hosted on the same domain) and display it in a div on the homepage, using jQuery.

在我的网站上,我尝试使用 jQuery 拉取我的论坛(托管在同一域中)中的帖子内容,并将其显示在主页上的 div 中。

Here's the code for the header:

这是标题的代码:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">

<script type="text/javascript">
jQuery("#contain").load("http://examplepage.com/forum/showthread.php?tid=NN #pid_NN");
</script>

Then, there's the div I'd like to display the post:

然后,是我想显示帖子的 div:

<div id="contain"></div>

Things to consider:

需要考虑的事项:

  • The library loads just fine.
  • If I enter any other code, it works (like testing alert(1);).
  • The console doesn't report any errors.
  • The div stays blank; in fact, it doesn't even show. It is there, though.
  • 该库加载得很好。
  • 如果我输入任何其他代码,它就可以工作(如测试警报(1);)。
  • 控制台不报告任何错误。
  • div 保持空白;事实上,它甚至没有显示。不过,它就在那里。

What am I doing wrong?

我究竟做错了什么?

采纳答案by rajesh kakawat

your code should be something like this

你的代码应该是这样的

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">

js code

js代码

 <script type="text/javascript">
   jQuery(document).ready(function(){
       jQuery("#contain").load("http://examplepage.com/forum/showthread.php?tid=NN #pid_NN");
   });
</script>

回答by Rhys Lees

.loadcan pass your GET params seperate:

.load可以单独传递您的 GET 参数:

.load("link to php", "http://examplepage.com/forum/showthread.php", "tid=NN#pid_NN")

回答by sjdaws

You need a closing </script>tag on your jQuery include, and you need to wait for dom load.

</script>的 jQuery 包含需要一个结束标记,并且您需要等待 dom 加载。

JS

JS

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery("#contain").load("http://examplepage.com/forum/showthread.php?tid=NN#pid_NN");
    });
</script>

HTML

HTML

<div id="contain"></div>

回答by Tsimtsum

Check this code. you should use ">*" after container id to load content specific container on the page.

检查此代码。您应该在容器 id 后使用“>*”来加载页面上的内容特定容器。

jQuery('#contain').load("http://example.com/pageurl #somecontaineronpage >*",function(){
      //after loading completion code goes here                      
});

Hope, this will help

希望,这会有所帮助