【发布时间】:2017-03-21 10:55:53
【问题描述】:
请参阅下面的代码,我想要实现的是,当用户单击“Div 1”时,菜单会滑动打开以选择 Web 1,但是单击该菜单时,菜单会关闭并且 iframe 会发生变化。
目前只有隐藏菜单或更改 iframe 有效,但不能同时使用。
我添加了第二个“Div 2”来显示 iframe 的变化。
<html>
<head>
<script src="https://code.jquery.com/jquery-3.2.0.js" integrity="sha256-wPFJNIFlVY49B+CuAIrDr932XSb6Jk3J1M22M3E2ylQ=" crossorigin="anonymous"></script>
<style>
.targetDiv {
display: none;
background-color: red;
width: 200px;
height: 100px;
display: none;
position: absolute;
left: 10px;
top: 50px;
}
.iframe {
bottom: 0px;
left: 0px;
position: absolute;
width: 500px;
height: 500px;
}
</style>
</head>
<body>
<div>
<a class="showSingle">Div 1</a>
</div>
<div>
<a class="showSingle">Div 2</a>
</div>
<div class="targetDiv">
<a href="http://www.skysports.com/" class="showSingle" target="myiframe">Web 1</a>
</div>
<div class="targetDiv">
<a href="http://www.bbc.co.uk/news" target="myiframe">Web 2</a>
</div>
<iframe src="http://www.bing.com/" name="myiframe" class="iframe"></iframe>
</body>
</html>
<script>
jQuery(function() {
jQuery('.showSingle').click(function() {
var index = $(this).parent().index(),
newTarget = jQuery('.targetDiv').eq(index);
jQuery('.targetDiv').not(newTarget).slideUp('fast')
newTarget.delay('fast').slideToggle('fast')
return false;
})
});;
</script>
【问题讨论】:
-
虽然您的问题不清楚,但一个快速的解决方案是将
class="showSingle"添加到具有文本Web2的a,您忘记了。
标签: javascript jquery html css iframe