简体   繁体   中英

Loading different content into a div

I am a little new to js and jQuery so bear with me.

I want to load different content into a div depending on what is clicked. If link A is clicked then content A is loaded, if link B is clicked then content B is loaded..etc etc. I have managed to get content into the div but cant get more than the first to work.

 <p class="pageid" data-page="page_a">Click here for a<p> 
<p class="pageid" data-page="page_b">Click here for b<p> 

<div id="new_content" style="width:500px; margin-top:50px;"></div>

<script>
//set class pageid as a variable
var pageid = $('.pageid')
//get pageid's data page info
var page=pageid.attr("data-page")

//onclick load page
$('.pageid').click(function () {
      $("#new_content").load(page);
    });

</script>

Can someone point me in the right direction please. Thanks Phil

not sure try this:

$('.pageid').click(function () {
      $("#new_content").load($(this).attr("data-page"));
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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