Jquery won't load content into the DIV area ..

2019-08-29 08:11发布

问题:

Ok , so I am trying to load content from a another page using JQUERY LOAD method SO THE content can appear without the page refreshing ... I did that but when I click on the link , it still redirects me to the other page instead of the content loading into the div.

Below is my code ..

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title> MadScore, A Social Scoring Platform </title>
<link rel="stylesheet" type="text/css" href="css/madscore.css">
<link rel="stylesheet" type="text/css" href="css/skins/tango/skin.css" />
<script type="text/javascript" src="javascript/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="javascript/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="javascript/jquery.jcarousel.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
    jQuery('#mycarousel').jcarousel({
        scroll: 1, buttonNextHTML:"<div></div>", buttonPrevHTML:"<div></div>"
    });
});
</script>


//Here is where I made the Jquery call for the div below with class "panel" but it won't load ..

<scrip type="text/javascript">
$(document).ready(function(){
  $("profile").click(function(){
    $(".panel").load("this.href");
  });
});
</script>
</head>

<body>
<div class="sliding-panel">
<div class="container">
<ul id="mycarousel" class="jcarousel-skin-tango">
<li>

<li>
<div class="panel"> //panel supposed to be triggered by Jquery
<h1> People </h1>
<?php
 database_connect();
$query = "select * from People";
$result = $connection->query($query);
$row_count =$result->num_rows;

for($i = 1; $i <= $row_count; $i++)
  {
   $row = $result->fetch_assoc();
    echo "<a href='/profile.php?id=".$row['ID']."' id='profile'><img src ='../".$row['Picture']."' width='100' height='100' /> </a>";

  }


?>
</div>
</li>
</ul>

回答1:

You are missing # while binding click event

$(document).ready(function(){
  $("#profile").click(function(){  // add # here
    $(".panel").load("this.href");
  });
}); 

Also use .on to bind event to dynamically added elements.



回答2:

First, your selector is missing #. Also, you are just passing a string to the load method: "this.href", you need to pass the actual link: $this.attr('href')

Regarding the issue with the link, you need to prevent the default action (which is the the click on a). For that you have two options:

<script type="text/javascript">
   $(function(){
    alert('on ready works!');
      $("#profile").click(function(event){
        $(".panel").load($this.attr('href'), function(){
          alert('ajax called finished');
        });             
        return false; // or event.preventDefault();
      });
    });
    </script>