Javascript onclick link text change

坚强是说给别人听的谎言 提交于 2019-12-12 03:29:13

问题


I'm working on a new page and for the mobile version I'm going to make a navigation toggle in order to hide and to show the navigation.

HTML code :

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

Javascript code :

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";
    }
}

I've tried with :

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

to change the text in text "Navigation Einblenden" when pressing on the link, but this doesn't work... Does somebody has an idea?


回答1:


Moved the id from div to the a.

See it working in jsbin http://jsbin.com/eqeheb/5/watch

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




回答2:


Make change like this....
HTML code

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

And JavaScript Code

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";
      }
}

This will work for you as its working fo



来源:https://stackoverflow.com/questions/13929016/javascript-onclick-link-text-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!