如何更改此JavaScript只使用1个链接 [英] How to change this javascript only to use only 1 link

查看:132
本文介绍了如何更改此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个链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆