简体   繁体   English

jQuery加载页面进入 <div> 问题

[英]jQuery load page into <div> issues

I'm using the following code to load a page into a <div> . 我正在使用以下代码将页面加载到<div> It works fine except it's not supposed to load all links into the <div> . 它工作正常,但它不应该将所有链接加载到<div>

<script type='text/javascript'>   
   var $j = jQuery.noConflict();
   $j(document).ready(function(){
        $j("a").click(function(){
             $j.ajax({
              url: $j(this).attr("href"),
              success: function(response) {
               $j("#output").html(response);
              }
             });
             return false;
        });
   });
</script>





 <div>[ <a href="execute.php?cmd=test1">link 1</a]  [ <a
href="execute.php?cmd=test2">link 2</a] [ <a href="mypage.com">link
3</a>]</div>


<div id="output"></div> 

When I click link 3 , used to go to the homepage, it opens the homepage in <div id="output"></div> . 当我点击link 3 (用于转到主页)时,它会打开<div id="output"></div>的主页。

Any idea why this happens? 知道为什么会这样吗?

You are assigning the click event to the a element, so all links will load the url inside the output div. 您将click事件分配给a元素,因此所有链接都将在输出div中加载url。 You need to specify which elements to assign the click event, maybe by specifying a class. 您需要指定要分配click事件的元素,可以通过指定类来指定。

$j("a.loadindiv").click


<a class="loadindiv" href

Hope this helps... 希望这可以帮助...

Link 3 is making a request to "mypage.com" and taking the html that it returns and outputting it into the 'output' div. 链接3正在向“mypage.com”发出请求并获取它返回的html并将其输出到“output”div。 You are making an AJAX call to the homepage and it is serving up the page to you. 您正在对主页进行AJAX调用,并且正在为您提供页面服务。

Rather than having all the elements use the AJAX function you might want to consider doing: 您可能需要考虑执行以下操作,而不是让所有元素都使用AJAX函数:

<script type='text/javascript'>   
   var $j = jQuery.noConflict();
   $j(document).ready(function(){
    $j(".ajaxLink").click(function(){
         $j.ajax({
          url: $j(this).attr("href"),
          success: function(response) {
           $j("#output").html(response);
          }
         });
         return false;
    });
 });
</script>





 <div>[ <a href="execute.php?cmd=test1" class="ajaxLink">link 1</a]  [ <a
href="execute.php?cmd=test2" class="ajaxLink">link 2</a] [ <a href="mypage.com">link
3</a>]</div>


<div id="output"></div> 

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

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