![]() |
Slide menu - show one div open on load.
Using JQuery, I have the following code:
PHP Code:
HTML Code:
<div class="menu">Demo of current situation: http://www.phpencoder.org/ Looking forward to suggestions/examples! |
instead of
PHP Code:
PHP Code:
HTML Code:
<body onload="openDiv('services')"> |
thing is, the js doesnt loop - how do I tag the div with the variablename?
|
Add the
slideToggle code to onload on <body> perhaps? |
I've no idea what the guys above are trying to do, but to solve your problem just requires a little lateral thinking. The problem is that your current code hides all of the div elements within the menu. Now, we want one of those divs to be open -- what should we do? Easy, open it! Using a nifty selector (jQuery has some really nice ones) we can grab hold of the correct div as easy as pie.
Here's the amended code, I've just added one line (with a comment). Code:
$(document).ready(function() {Edit: After reading the jQuery documentation, it's possible to select all of the DIV elements not belonging to the Services heading by modifying your original selector. The modification uses the handy not and contains filters. PHP Code:
|
Perfect, thanks Salathe!
|
One more question, how can I call the h3 tag that I want opened? Eg. in my HTML file I have the following: menu.js?show=Services for Services?
|
At first, I thought this was going to be a nightmare but thankfully I was wrong! That said, it's not easy peasy so I'll show you the amended code first and explain what's going on later.
PHP Code:
?show=something) or if that item does not exist in your menu. The line starting var szDiv... stores a default selector to use in these cases. I'm not sure if you'll understand the selector syntax but basically by default we'll be telling everything but the first menu item to hide itself.The next line, starting var aShow... is pretty complicated but shows the power of frameworks like jQuery. I'll go through step by step. $('head > script[src^="menu.js"]:first') is another complicated selector but don't let that put you off. All we're doing here is grabbing the first (if any) element in the HTML document which is a SCRIPT element within the HEAD of your HTML and has the SRC attribute pointing to our menu.js file. The next part, attr('src'), simply grabs the SRC attribute from that SCRIPT element. With match(/show=([^&]*)/) we text that SRC attribute against a regular expression which pulls out the provided show, if there is one.The next line (the if statement) just checks to see whether we could actually pull out the item to show or not. If we could grab an item to show, then we change szDiv to a new selector which makes jQuery display that item rather than the default.$(szDiv).hide(); then puts all of the above into action by hiding items which match the szDiv selector. This results in all of the divs being hidden except our chosen one, or the first one (default) if no item is chosen.The rest of the script is identical to before, simply assigning something to do when the headings are clicked. |
| All times are GMT. The time now is 07:59 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0