Javascript onclick链接文本更改

时间:2012-12-18 08:29:14

标签: javascript text hyperlink onclick

我正在开发一个新页面,对于移动版本,我将进行导航切换以隐藏和显示导航。

HTML code:

<div id="toogleNavigation">
    <a onclick="toggle_visibility('nav_header','nav_header_level2');">Navigation Einblenden</a>
</div>

Javascript代码:

function toggle_visibility(id, id2) {
    var e = document.getElementById(id);
    var f = document.getElementById(id2);
    if(e.style.display == 'block' || 
       e.style.display == 'block' && 
       f.style.display == 'block') {
        e.style.display = 'none';
        f.style.display = 'none';
        document.getElementById('toogleNavigation').innerHTML = "Navigation 1einblenden";
    } else {
        e.style.display = 'block';
        f.style.display = 'block';
        document.getElementById('toogleNavigation').innerHTML = "Navigation 2ausblenden";
    }
}

我试过了:

document.getElementById('toogleNavigation').innerHTML = "Navigation 2ausblenden";

在按下链接时更改文本“Navigation Einblenden”中的文本,但这不起作用...有人有想法吗?

2 个答案:

答案 0 :(得分:2)

将id从div移到a。

在jsbin http://jsbin.com/eqeheb/5/watch

中查看它

<div> <a id="toogleNavigation" onclick="toggle_visibility('nav_header','nav_header_level2');">Navigation Einblenden</a> </div>

答案 1 :(得分:0)

像这样改变....
HTML代码

<div id="toogleNavigation">
    <a id="divInner" onclick="toggle_visibility('nav_header','nav_header_level2');">
        Navigation Einblenden
    </a>
</div>

JavaScript代码

function toggle_visibility(id, id2) {
   var e = document.getElementById(id);
   var f = document.getElementById(id2);
   if(e.style.display == 'block' ||
      ( e.style.display == 'block' &&            
      f.style.display == 'block' ) ) {
        e.style.display = 'none';
        f.style.display = 'none';
        document.getElementById('divInner').innerHTML = "Navigation  1einblenden";
      } 
      else {
        e.style.display = 'block';
        f.style.display = 'block';
        document.getElementById('divInner').innerHTML = "Navigation 2ausblenden";
      }
}

这对您有用正在工作