Link back out from child HTML loaded in div to parent HTML

扶醉桌前 提交于 2019-12-11 12:54:43

问题


I have an id'd part of a child HTML file loaded into the parent HTML's div. I have a button at the top to empty out the child content and return the div to the parent content that was there previously. This is for an image gallery, with a main navigation (parent) and then the detailed view with smaller navigation (child). Here is the parent HTML, index.html (with CSS and JS embedded):

<html>
<head>
<title>Java Factory</title>

<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>

<style type="text/css">

#container {
width: 1020px;
height: 634px;
}
ul#flavors {
list-style: none;
width: 1020px;
height: 634px;
position: relative;
background: url(images/coffee/thumbs_large.jpg) no-repeat 0 0;
}
ul#flavors li {
position: absolute;
}
.wakey {
width: 309px;
height: 309px;
top: 0px;
left: 30px;
}
.smooth {
width: 309px;
height: 309px;
top: 0px;
left: 355px;
}
ul#flavors li a  {
display: block;
outline: none;
height: 100%;
}
ul#flavors li a  {
text-indent: -9000px;
}
ul#flavors li a:hover {
background: url(images/coffee/thumbs_large.jpg) no-repeat 0 0;    
}
ul#flavors li.wakey a:hover {
background-position: -30px -640px;
}
ul#flavors li.smooth a:hover {
background-position: -355px -640px;
}

#top {
height: 36px;
margin-top: 10px;
margin-bottom: 10px;
}

</style>

</head>
<body>

<div id="web">

<div id="top">
</div>

<nav id="container">
<ul id="flavors" class="coffeenav">
<li class="wakey"><a href="#wakey">Wakey Wakey</a></li>    
<li class="smooth"><a href="#smooth">Smooth Caffeinator</a></li>
</ul>
</nav>
</div>

<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
$('#web').on('click', '.coffeenav li a', function () {
$('#web').load('coffee.html #' + $(this).attr('href'));
return false;
});
});
</script>

</body>
</html>

And here is the child HTML:

<html>
<head>
    <title>div container switch test</title>

<style type="text/css">
#coffee_return {
height: 36px;
margin-top: 10px;
margin-bottom: 10px;
}
</style>

</head>
<body>

<div id="wakey">

<style type="text/css">
.shell {
width:100%;
height:100%;
}
#container1 {
width: 1020px;
height: 624px;
background: url(images/coffee/wakey.jpg) no-repeat 0 0;
}
ul#flavors1 {
list-style: none;
width: 1020px;
height: 624px;
position: relative;
}
ul#flavors1 li {
position: absolute;
}
.wakey {
width: 159px;
height: 169px;
top: 455px;
left: 30px;
}
.smooth {
width: 159px;
height: 169px;
top: 455px;
left: 188px;
}
ul#flavors1 li a  {
display: block;
outline: none;
height: 100%;
}
ul#flavors1 li a  {
text-indent: -9000px;
}
ul#flavors1 li a:hover {
background: url(images/coffee/thumbsml_rollover.jpg) no-repeat 0 0;    
}
ul#flavors1 li.wakey a:hover {
background-position: 1px 11px;
}
ul#flavors1 li.smooth a:hover {
background-position: -157px 11px;
}
#coffee_return {
margin-top: 10px;
margin-bottom: 10px;
}
</style>

<div class="shell">

<div id="coffee_return">
<a href="x"><img src="images/coffee/return_btn.jpg" border="0"></a>
</div>

<nav id="container1">
<ul id="flavors1" class="coffeenav">       
    <li class="smooth"><a href="#smooth">Smooth Caffeinator</a></li>
    <li class="vanilla"><a href="#vanilla">Vanilla Dream</a></li>
</ul>
</nav>

</div>
<div class="clr"></div>

</div>
<div class="clr"></div>



<div id="smooth">

<style type="text/css">
.shell {
width:100%;
height:100%;
}
#container2 {
width: 1020px;
height: 624px;
background: url(images/coffee/smooth.jpg) no-repeat 0 0;
}
ul#flavors2 {
list-style: none;
width: 1020px;
height: 624px;
position: relative;
}
ul#flavors2 li {
position: absolute;
}
.wakey {
width: 159px;
height: 169px;
top: 455px;
left: 30px;
}
.smooth {
width: 159px;
height: 169px;
top: 455px;
left: 188px;
}
ul#flavors2 li a  {
display: block;
outline: none;
height: 100%;
}
ul#flavors2 li a  {
text-indent: -9000px;
}
ul#flavors2 li a:hover {
background: url(images/coffee/thumbsml_rollover.jpg) no-repeat 0 0;    
}
ul#flavors2 li.wakey a:hover {
background-position: 1px 11px;
}
ul#flavors2 li.smooth a:hover {
background-position: -157px 11px;
}
</style>

<div class="shell">

<div id="coffee_return">
<a href="x"><img src="images/coffee/return_btn.jpg" border="0"></a>
</div>

<nav id="container2">
<ul id="flavors2" class="coffeenav">       
    <li class="wakey"><a href="#wakey">Wakey Wakey</a></li>
    <li class="vanilla"><a href="#vanilla">Vanilla Dream</a></li>
</ul>
</nav>

</div>
<div class="clr"></div>

</div>
<div class="clr"></div>



</body>
</html>

The button inside each referenced div id in the child HTML is this:

<div id="coffee_return">
<a href="x"><img src="images/coffee/return_btn.jpg" border="0"></a>
</div>

And the demo for this is: http://mmdsgn.com/divsample/5/ -- You'll see the return button appears at the top when you click either of the first two boxes (only ones that work right now) and I need that graphic button to call up the original div id content in the parent HTML.


回答1:


Change the path on "href" to "../" instead of "x"

<a href="../"><img src="images/coffee/return_btn.jpg" border="0"></a>

or remove it. Using # is not really recommended since your script looks for the content of href. So leaving it empty would cause the page to refresh? i'm not quite sure but it works.

<a href=""><img src="images/coffee/return_btn.jpg" border="0"></a>

Edit: Now that i think about it. The first one would not work on your page since you are in the same folder. ;-)




回答2:


You'll want to change your original script a bit:

<script type="text/javascript" charset="utf-8">
$(document).ready(function () {
$('#web').on('click', '.coffeenav li a', function () {
$('#web').load('coffee.html #' + $(this).attr('href'),
function() {
    $('#coffee_return').on('click', function () {
      $('#web').load('./ #web');
    return false;
});
});
return false;
});
});
</script>

This basically says: After loading a section of coffee.html, look for a coffee_return button and add onclick behavior to re-load the original page into your #web section.

Also, change that href on your coffee_return button to # or JavaScript:void(0); since it's trying to load a document called "x" currently:

<div id="coffee_return">
<a href="#"><img src="images/coffee/return_btn.jpg" border="0"></a>
</div>



回答3:


You cannot use same id on multiple elements, as you said "

The button inside each referenced div id in the child HTML is this:

<div id="coffee_return">
<a href="x"><img src="images/coffee/return_btn.jpg" border="0"></a>
</div>

". This would give erroneous results. Rather, assign them a class and bind event with their class.



来源:https://stackoverflow.com/questions/20579323/link-back-out-from-child-html-loaded-in-div-to-parent-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!