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

javascript - Simple jquery toggle (Not Working) - Stack Overflow

programmeradmin0浏览0评论

I think I may be using the wrong js library. No idea.. I was following a tutorial online and I saw the guy code it, and watch it work. I reviewed twice and did it over and still didn't work.

Here's my html:

<html lang="en">
<head>
<meta charset="utf-8" />
<title>Title</title>
 <script src="//ajax.googleapis/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  <script src="hideshow.js"></script>

</head>
<body>

<a href="#" id="hideshow">Hide </a>

<div id="message" style="width:300px; border:1px solid black; padding:10px">

This is the wording inside of the text for testing purposes.

</div>
</body>

Here's my js from hideshow.js:

$('#hideshow').toggle(function () {

$('#hideshow').text('Show');

}, function () {

$('#hideshow').text('Hide');
});

I appreciate it, Thanks for whoever can figure this out. :(

I think I may be using the wrong js library. No idea.. I was following a tutorial online and I saw the guy code it, and watch it work. I reviewed twice and did it over and still didn't work.

Here's my html:

<html lang="en">
<head>
<meta charset="utf-8" />
<title>Title</title>
 <script src="//ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  <script src="hideshow.js"></script>

</head>
<body>

<a href="#" id="hideshow">Hide </a>

<div id="message" style="width:300px; border:1px solid black; padding:10px">

This is the wording inside of the text for testing purposes.

</div>
</body>

Here's my js from hideshow.js:

$('#hideshow').toggle(function () {

$('#hideshow').text('Show');

}, function () {

$('#hideshow').text('Hide');
});

I appreciate it, Thanks for whoever can figure this out. :(

Share asked Feb 23, 2014 at 18:22 some guy named davesome guy named dave 111 silver badge2 bronze badges 1
  • 1 The toggle event was removed from jQuery 1.9. See api.jquery./toggle-event – j08691 Commented Feb 23, 2014 at 18:27
Add a ment  | 

3 Answers 3

Reset to default 5

Looks like you're trying to use the toggle event, which was deprecated in jQuery 1.8 and removed on jQuery 1.9. The new toggle function has many different overloads, but none that behaves like the old one. Take a look at it and choose the one that suits you the most. For example, you could do something like this:

$('#hideshow').click(function () {
    $('#message').toggle();
    if($('#message').is(':visible')) {
        $('#hideshow').text('Hide');        
    } else {
        $('#hideshow').text('Show');   
    }
});

And here's a jsfiddle for you to play with. http://jsfiddle/74GzV/1/

As j08691 pointed out, toggle is removed, but even if it wasn't <script src="hideshow.js"></script> is running before the page has loaded, and therefore #hideshow would turn up undefined. Try putting the whole thing in a $(document).ready after working around the lack of toggle

var toggler = $('#hideshow');
toggler.text('Hide');

toggler.click(function(e) {
    e.preventDefault();
    $('#message').slideToggle('slow', function () {
      var togglerLabel = $(this).is(':visible') ? 'Hide' : 'Show';
      toggler.text(togglerLabel);
    });
});

http://jsfiddle/4TqQT/46/

发布评论

评论列表(0)

  1. 暂无评论