[英]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的另一个div标签中获取一个div标签,我正在将另一个页面的内容加载到div标签中,因此我需要获取div标签才能将该页面加载到其中,还需要将div标签添加到当用户单击菜单图像时触发脚本。
Javascript Code JavaScript代码
$('#menu-body a').click(function() {
var page = $(this).attr('href');
$('#body-wrap').load('content/' + page + '.php');
return false;
});
HTML Code HTML代码
<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. body-wrap是您的html中的类。 meanwhile you state body-wrap as an id in your javascript code.
同时,您在JavaScript代码中将body-wrap声明为ID。 change
$("#body-wrap")
to $(".body-wrap")
, or <div class="body-wrap"></div>
to <div id="body-wrap"></div>
将
$("#body-wrap")
更改$(".body-wrap")
,或将<div class="body-wrap"></div>
更改为<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. 也必须使用
$(".menu-box a")
而不是$("#menu-body a")
来代替html中没有带有menu-body id的div。
change your code to below one. 将您的代码更改为以下代码。
#body-wrap represent id body-wrap of div #body-wrap代表div的id主体包装
<div id="body-wrap">
.body-wrap represent class body-wrap of div .body-wrap表示div的类body-wrap
<div class="body-wrap">
$('.body-wrap').load('content/' + page + '.php');
At first, try to get an idea on class
and id
selectors in CSS. 首先,尝试了解CSS中的
class
和id
选择器。
An id is selected in CSS using #
and a class using .
在CSS中使用
#
选择ID,并使用在类中选择ID .
infront. 在前。
Check this CSS CLASS & ID 检查此CSS CLASS&ID
Here in your HTML , 在您的HTML中 ,
body-wrap
is a class, So you have to select it by using .body-wrap
. body-wrap
是一类,因此您必须使用.body-wrap
进行选择。
There is no elements with id menu-body
in your HTML. HTML中没有ID为
menu-body
元素。 So #menu-body
used in your jQuery is an invalid selector. 因此,在jQuery中使用的
#menu-body
是无效的选择器。 You may use $(".menu-box a")
to select anchor tags inside .menu-box
您可以使用
$(".menu-box a")
在.menu-box
选择定位标记
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.