I can't figure out why the following code doesn't work on my site, but works great on JSFiddle, as well as here in a Stack Snippet:
(function($) {
$(document).ready(function() {
$
$(".toggler").click(function() {
$(this).next().slideToggle("slow");
}).next().hide();
$(".togglerLink").click(function() {
$(this).nextAll('.toggled:first').fadeIn("fast");
});
});
})(jQuery)
.toggler {
color: orange;
text-decoration: underline
}
.toggler:hover {
color: orange;
cursor: pointer;
text-decoration: none
}
<script src="http://ift.tt/1oMJErh"></script>
The <a class="togglerLink" href="#link">link</a> here, has a destination inside the Toggler.
<br>
<br>
<p class="toggler">Toggle here.</p>
<div class="toggled">In JSFIddle, or in a Stack Snippet (SO), this code is working fine. Even when the Toggler is closed, the link automatically opens the Toggle that contains it destination. --- So, then what could be going wrong, implementing this into a website?
<br>
<br><span id="link" style="color:green">Link-destination.</span>
<hr>
</div>
What is not working then is:
- The link doesn't automatically opens the required Toggle using
$(this).nextAll('.toggled:first').fadeIn("fast");
. Thus, the link leads nowhere when the Toggler is closed.
Still working:
Manual toggling with jQuery
$(this).next().slideToggle("slow"); }).next().hide();
still works fine.Also, the link
<a class="togglerLink" href="#link">link</a>
works fine when the toggler is opened manually.
Literature look-up
I tried to kill this bug by looking up similar posts:
→: Is jQuery included twice? → I think not.
→: Don't forget about some brackets.
→: Use the jQuery
(document).ready(
-function.- Perhaps the weird behavior has something to do with the brackets or the jQuery document ready function $(?
Normally, I load my jQuery-codes in between <script type="text/javascript">(function($){$(document).ready(function(){$
...;});})(jQuery)</script>
.
However, here, in this "problematic" code, I have to use });})(jQuery)
to close. Thus with 1 ;
less up front.
Debugging
- Firefox 35.0.1.
jQuery-1.10.2.min.js :1 (this also occurs on test pages without the "problematic" code)
Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead
☞ jQuery-1.10.2.min.js :5 → This only shows when clicking the link, when the Toggler is closed!
Use of getPreventDefault() is deprecated. Use defaultPrevented instead.
- Chrome 40.0... (View > Developer > Developer Tools > Console) gives no warnings.
Aucun commentaire:
Enregistrer un commentaire