不是太难,只是改变iframe的src标签。假设以下是您的 html:
<body>
<!-- Assuming 1.html is the first shown, you can
initialize it as the first src to be displayed -->
<iframe id="frame" src="1.html"></iframe>
<a href="#" id="prev">Previous</a> <a href="#" id="next">Next</a>
</body>
然后,您可以按照以下说明进行操作:
jQuery
var currentView = 1,
minView = 1,
maxView = 4;
var changeViewNext = function() {
if (currentView >= minView && currentView < maxView) {
currentView++;
$('#frame').prop('src', currentView + '.html');
}
}
var changeViewPrev = function() {
if (currentView <= maxView && currentView > minView) {
currentView--;
$('#frame').prop('src', currentView + '.html');
}
}
// Then some click events
$('#prev').click(function(e) {
e.preventDefault();
changeViewPrev();
});
$('#next').click(function(e) {
e.preventDefault();
changeViewNext();
})
或者,只需将标记添加到您的锚标记以调用这些函数:
<a href="#" onClick="changeViewPrev()">Previous</a>
<a href="#" onClick="changeViewNext()">Next</a>
更新
如果您想使用按钮而不是锚标签,请保持 jQuery 不变,只需更改按钮的锚标签,就像这样
<button id="prev">Previous</button> <button id="next">Next</button>
UPDATE2:仅使用 javascript 测试代码,没有 jQuery
<body>
<!-- Assuming 1.html is the first shown, you can
initialize it as the first src to be displayed -->
<iframe id="frame" src="1.html"></iframe>
<button onClick="changeViewPrev()">Previous</button>
<button onClick="changeViewNext()">Next</button>
<script>
var currentView = 1,
minView = 1,
maxView = 4,
frame = document.getElementById('frame');
function changeViewNext() {
if (currentView >= minView && currentView < maxView) {
currentView++;
frame.src = currentView + '.html';
}
}
function changeViewPrev() {
if (currentView <= maxView && currentView > minView) {
currentView--;
frame.src = currentView + '.html';
}
}
</script>
</body>