最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to hideshow div based on variable using javascript - Stack Overflow

programmeradmin7浏览0评论

I am trying to display a div based on the URL parameter. I have to use only html css and javascript. I got it working until the part where the div must be set to display none if the variable (parameter from URL) matches.

HTML

   <div id="cfiblinks">
     <div class="row">
       <div class="twelve columns">
         <ul class="nav-bar">
           <li>
             <a href="#" target="_blank">
               <span>Order Document Upload</span>
             </a>
           </li>
           <li>
             <a href="#" target="_blank">
               <span>Business Card History</span>
             </a>
           </li>
           <li>
             <a href="#" target="_blank">   
               <span>Material Inventory</span>
             </a>
           </li>
         </ul>
        </div>
     </div>
   </div>

Javascript

<script language="JavaScript">

    function getURLParameter(name) {
        return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[""])[1].replace(/\+/g, '%20'))||null
    }

    myvar = getURLParameter('UserGroupId');

    document.write('The url parameter is: ' + myvar +'       ');

    if (myvar == 10102) {
        document.write('The url parameter is: ' + myvar +'     ');
        document.getElementById('cfiblinks').style.visibility = 'visible';
    } else {
        document.write('The url parameter is not : ' + myvar +'      ');
        document.getElementById('cfiblinks').style.visibility = 'hidden';
    }


</script>

Any help would be greatly appreciated. I suspect something is wrong with the document.getElementById.

I am trying to display a div based on the URL parameter. I have to use only html css and javascript. I got it working until the part where the div must be set to display none if the variable (parameter from URL) matches.

HTML

   <div id="cfiblinks">
     <div class="row">
       <div class="twelve columns">
         <ul class="nav-bar">
           <li>
             <a href="#" target="_blank">
               <span>Order Document Upload</span>
             </a>
           </li>
           <li>
             <a href="#" target="_blank">
               <span>Business Card History</span>
             </a>
           </li>
           <li>
             <a href="#" target="_blank">   
               <span>Material Inventory</span>
             </a>
           </li>
         </ul>
        </div>
     </div>
   </div>

Javascript

<script language="JavaScript">

    function getURLParameter(name) {
        return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[""])[1].replace(/\+/g, '%20'))||null
    }

    myvar = getURLParameter('UserGroupId');

    document.write('The url parameter is: ' + myvar +'       ');

    if (myvar == 10102) {
        document.write('The url parameter is: ' + myvar +'     ');
        document.getElementById('cfiblinks').style.visibility = 'visible';
    } else {
        document.write('The url parameter is not : ' + myvar +'      ');
        document.getElementById('cfiblinks').style.visibility = 'hidden';
    }


</script>

Any help would be greatly appreciated. I suspect something is wrong with the document.getElementById.

Share Improve this question edited May 7, 2014 at 15:10 MythRaven asked May 7, 2014 at 14:48 MythRavenMythRaven 812 gold badges2 silver badges7 bronze badges 5
  • 1 offtopic: i would suggest using console.log for debugging instead of document.write – lordvlad Commented May 7, 2014 at 15:02
  • 1 your code does work for me on jsbin (jsbin./yejowixo/1/edit) maybe you should open the developer console (ctrl+shift+j on chrome, ctrl+shift+k on ff) and have a look what's happening. try running getURLParameter('UserGroupId') from the console – lordvlad Commented May 7, 2014 at 15:12
  • 1 I guess the reason is that your code runs BEFORE the DOM tree is loaded. You would want to use jQuery or similar anyway; or at least place the script tag at the end of the document – Antti Haapala -- Слава Україні Commented May 7, 2014 at 15:20
  • interesting. that is very odd. In ff, i get the following errors: - Use of getUserData() or setUserData() is deprecated. Use WeakMap or element.dataset instead. - TypeError: document.getElementById(...) is null - An unbalanced tree was written using document.write() causing data from the network to be reparsed. For more information developer.mozilla/en/… – MythRaven Commented May 7, 2014 at 15:25
  • YESS!!!! I moved it to the end of the document and it works like a charm! Thank you for clarifying that antti.... +1 for all of yous. As soon as i figure out how! :p – MythRaven Commented May 7, 2014 at 15:28
Add a ment  | 

3 Answers 3

Reset to default 2

You can set the display property using block or none.

document.getElementById('cfiblinks').style.display = 'block'; //Will show
document.getElementById('cfiblinks').style.display = 'none'; //Will hide

Try this..

if (myvar == 10102) {

    document.write('The url parameter is: ' + myvar +'     ');
    document.getElementById('cfiblinks').setAttribute("style", "display:block");

} else {
   document.write('The url parameter is not : ' + myvar +'      ');
   document.getElementById('cfiblinks').setAttribute("style", "display:none");
  }

Try:

document.getElementById('something').style.display = "block";


and

document.getElementById('something').style.display = "none";


发布评论

评论列表(0)

  1. 暂无评论