Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用按钮循环浏览html文件列表_Javascript_Html_Cycle - Fatal编程技术网

Javascript 使用按钮循环浏览html文件列表

Javascript 使用按钮循环浏览html文件列表,javascript,html,cycle,Javascript,Html,Cycle,我有一份产品清单,上面写着: laptops/prod1.html laptops/prod2.html laptops/prod3.html monitors/prod1.html monitors/prod2.html monitors/prod3.html 我想在我的页面上有一个按钮,可以“循环”浏览可用的项目 不知道怎么做。这在javascript中是可能的吗 function nextProduct(incr) { var href = window.location.href

我有一份产品清单,上面写着:

laptops/prod1.html
laptops/prod2.html
laptops/prod3.html
monitors/prod1.html
monitors/prod2.html
monitors/prod3.html
我想在我的页面上有一个按钮,可以“循环”浏览可用的项目

不知道怎么做。这在javascript中是可能的吗

function nextProduct(incr) {
  var href = window.location.href
    , offset = (typeof(incr)==='undefined' ? 1 : incr);
  window.location = href.replace(/(\d+)\.html/, function(m, g1) {
    return (Number(g1) + offset) + '.html'
  });
}
然后,您可以执行以下操作:

var button;
button = document.getElementByID('next-button');
button.addEventListener('click', function() { nextProduct(1); });
button = document.getElementByID('prev-button');
button.addEventListener('click', function() { nextProduct(-1); });

设置主页时,这不应是静态html页面,而应使用您选择的服务器端语言

使用脚本标记将jquery包含到主页面(您可以从中获取jquery)

您的html可能如下所示:

<div id='content'></div>
<div>
   <a href='javascript:void(0)' id='prev' class='btn'>Previous</a>
   <a href='javascript:void(0)' id='next' class='btn'>Next</a>
</div>
var currPage = 0;
var pageList = ["laptops/prod1.html","laptops/prod2.html", "laptops/prod3.html"];
var totalPages = pageList.length;
$(".btn").on("click",function(){
   //if we are at the last page set currpage = 0 else increment currPage.
   currPage = currPage < (totalPages - 1) ? ++currPage : 0;
   var page = pageList[currPage];
   $('#content').load(currPage);
});

在js文件中,您将有如下内容:

<div id='content'></div>
<div>
   <a href='javascript:void(0)' id='prev' class='btn'>Previous</a>
   <a href='javascript:void(0)' id='next' class='btn'>Next</a>
</div>
var currPage = 0;
var pageList = ["laptops/prod1.html","laptops/prod2.html", "laptops/prod3.html"];
var totalPages = pageList.length;
$(".btn").on("click",function(){
   //if we are at the last page set currpage = 0 else increment currPage.
   currPage = currPage < (totalPages - 1) ? ++currPage : 0;
   var page = pageList[currPage];
   $('#content').load(currPage);
});
var currPage=0;
var pageList=[“lappeps/prod1.html”、“lappeps/prod2.html”、“lappeps/prod3.html”];
var totalPages=pageList.length;
$(“.btn”)。在(“单击”,函数(){
//如果我们在最后一页,则设置currpage=0,否则增加currpage。
currPage=currPage<(总页数-1)?++currPage:0;
变量页面=页面列表[当前页面];
$('#content').load(currPage);
});
需要考虑的几点:

  • 您需要决定是在主页加载还是单击加载第一页
  • 您需要设置一个js变量来跟踪当前加载的页面
  • 您需要添加一些存储所有可能页面的方法(考虑数组)。这可以在页面加载时打印到页面上的脚本标记
  • 你需要决定当你到达终点时会发生什么。您可以循环使用或灰显相应的链接

  • 你是什么意思?像是带有超链接的下拉列表吗?不只是一个按钮,它会将您带到下一个产品或上一个产品。您是在iframe中显示页面,还是将内容放入div,还是希望加载整个页面?你需要更具体一些,因为这些细节会有所不同。按钮在哪里?到目前为止,您尝试了什么?1)您正在尝试更改整个页面2)这些只是模板html,这意味着它们可以插入到主页的正文中3)是否要使用iframes。。。需要一些实际的代码来帮助您。(看到主题了吗?)-忍者-是的,我看到主题了。在这件事上,我的头被压得喘不过气来-放弃试图改变整个页面。(目前)不想使用iframesxsltproc失败,因为js:currPage=currPage的第6行<(totalPages-1)++第页:0;我不熟悉xsltproc,但您应该能够转义该字符。这可能有助于逃跑