本文介绍了如何更改此JavaScript只使用1个链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有javascript: - 当我们点击主页链接时,它显示主页内容,然后当我们点击关于链接时,它会显示相关内容。如何使这只有一个按钮,1按钮应该能够改变点击链接。这里是代码。
< script type =text / javascript> function show(page){ var html =; switch(page){ casehome: html ='页面的所有内容'; 休息; caseabout: html ='所有pahe的东西'; 休息; casecontact: html =这是联系页面< br /> ...; 休息; } document.getElementById('container')。innerHTML = html; < / script>工作小提琴 其他建议很好,但是switch语句, IMO,过度使用,混乱并导致许多不必要的错误。这是使用对象的较短替代方案。
function show(page){ var content = { home:'页面的所有内容< br /> ...', about:'关于页面的所有内容< br /> ...', contact:'这是联系页面< br /> ...'} document.getElementById('container')。innerHTML = content [page]; }
I have javascript:- when we click on the 'home' link it shows home stuff,then when we click the 'about' link it shows the about related things. How to make this only to have one button and that 1 button should be able to change links on clicks. Here is the code.
<script type = "text/javascript" > function show(page) { var html = ""; switch (page) { case "home": html = 'All the stuff of the page'; break; case "about": html = 'All the stuff of the pahe'; break; case "contact": html = "This is the contact page<br />..."; break; } document.getElementById('container').innerHTML = html; </script>A working fiddle.
解决方案The other suggestions are good, but switch statements, IMO, are overused, confusing and lead to many unnecessary bugs. Here's a shorter alternative using an object instead.
function show(page) { var content = { home : 'All the stuff of the page<br />...', about : 'All the stuff about the page<br />...', contact : 'This is the contact page<br />...' } document.getElementById('container').innerHTML = content[page]; }
更多推荐
如何更改此JavaScript只使用1个链接
发布评论