简体   繁体   中英

Getting a div inside a div

I'm trying to get a div tag inside of another div tag from a javascript, I'm loading content from another page into a div tag so I need to get the div tag to load the page into and also get the div tag to trigger the script when a user clicks on a menu image.

Javascript Code

$('#menu-body a').click(function() {
    var page = $(this).attr('href');
    $('#body-wrap').load('content/' + page + '.php');
    return false;
});

HTML Code

<head>
    <link rel="stylesheet" type="text/css" href="css/style.css" >
    <title>End of Us - Home</title>

    <script type="text/javascript">
        function MM_swapImgRestore() { //v3.0
            var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
        }
        function MM_preloadImages() { //v3.0
            var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
            var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
            if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
        }

        function MM_findObj(n, d) { //v4.01
            var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
                d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
            if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
            for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
            if(!x && d.getElementById) x=d.getElementById(n); return x;
        }

        function MM_swapImage() { //v3.0
            var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
            if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
        }
    </script>

</head>

<body onLoad="MM_preloadImages('img/home_over.png','img/store_over.png','img/support_over.png','img/forum_over.png')">

    <div class="page-wrap">
        <div class="menu-wrap">
            <div class="menu-box"><a href="index" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('home','','img/home_over.png',1)"><img src="img/home.png" width="134" height="65" id="home"></a><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('store','','img/store_over.png',0)"><img src="img/store.png" width="144" height="65" id="store"></a><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('support','','img/support_over.png',1)"><img src="img/support.png" width="190" height="65" id="support"></a><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('forums','','img/forum_over.png',1)"><img src="img/forum.png" width="142" height="65" id="forums"></a></div>
        </div>

        <div class="body-wrap"></div>

        <div class="footer-wrap">
            <div class="footer-box">End of Us - Site by Gawdzahh</div>
        </div>
    </div>

    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="js/contentLoader.js"></script>
</body>

body-wrap is a class in your html. meanwhile you state body-wrap as an id in your javascript code. change $("#body-wrap") to $(".body-wrap") , or <div class="body-wrap"></div> to <div id="body-wrap"></div>

also instead of $("#menu-body a") you must use $(".menu-box a") There is no div with id of menu-body in your html.

change your code to below one.

#body-wrap represent id body-wrap of div

<div id="body-wrap">

.body-wrap represent class body-wrap of div

 <div class="body-wrap">

  $('.body-wrap').load('content/' + page + '.php');

At first, try to get an idea on class and id selectors in CSS.

An id is selected in CSS using # and a class using . infront.

Check this CSS CLASS & ID

Here in your HTML ,

  1. body-wrap is a class, So you have to select it by using .body-wrap .

  2. There is no elements with id menu-body in your HTML. So #menu-body used in your jQuery is an invalid selector. You may use $(".menu-box a") to select anchor tags inside .menu-box

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