如何更改此JavaScript只使用1个链接

编程入门 行业动态 更新时间:2024-10-25 00:34:22
本文介绍了如何更改此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个链接

本文发布于:2023-11-22 05:28:56,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何更改   链接   JavaScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!