I have an HTML code:
<html>
<head>
<style>
.selected {
color: red;
}
#myContainer div {
border: 1px solid #333;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// array of links
var listItems = $('#myList li a');
// array of divs contain content (called containers)
var containers = $('#myContainer > div');
// hide all containers
containers.hide();
// listen for click event on links
listItems.click(function(e){
// prevent link default action
e.preventDefault();
// hide all containers
containers.hide();
// show the container that has id like value of link's hash
containers.filter(this.hash).show();
// remove selected class on all links
listItems.removeClass('selected');
// add selected class on this link
$(this).addClass('selected');
return false;
}).filter(':first').click(); // default the first link is active
});
</script>
</head>
<body>
<ul id="myList">
<li><a href="#first">This is the 1st link</a></li>
<li><a href="#second">This is the 2nd link</a></li>
<li><a href="#third">This is the 3rd link</a></li>
</ul>
<div id="myContainer">
<div id="first">xxx</div>
<div id="second">yyy</div>
<div id="third">zzz</div>
</div>
</body>
</html>
When you click on one of three link above, the content of the corresponding div (one of three direct children of div#myContainer) will be shown.
But let's say that the HTML is returned from an certain AJAX call. So the click
event maybe not work. I remember that a selector resulted from an AJAX call must use something like
$(selector).live('click', function(){
// do stuff here
})
not
$(selector).click()
So, what should I do in this case to keep all behaviors as expected when HTML code resulted from an AJAX call, not static HTML?
Thanks