Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

jquery - Need helping getting this string of Javascript to work

Here is what I am working with:

  <script>

  $(document).ready(function() {

  $("#cf7_controls").on('click', 'span', function() {

  $("#cf7 img").removeClass("opaque");

  var newImage = $(this).index();

  $("#cf7 img").eq(newImage).addClass("opaque");

  $("#cf7_controls span").removeClass("selected");

  $(this).addClass("selected");

  });

  });

  </script>   <div class="row">
    <div class="span 6">
        <div id="cf7" class="shadow">
            <img class='opaque' src="/img/Cirques.jpg" />
            <img src="/img/ClownFish.jpg" />
            <img src="/img/Stones.jpg" />
            <img src="/img/Summit.jpg" />
        </div>
        <p id="cf7_controls">
            <span class="selected">Image 1</span>
            <span>Image 2</span>
            <span>Image 3</span>
            <span>Image 4</span>
        </p>
    </div>
    <div class="span 6">
        <div id="cf7" class="shadow">
            <img class='opaque' src="img/Cirques.jpg" />
            <img src="img/ClownFish.jpg" />
            <img src="img/Stones.jpg" />
            <img src="img/Summit.jpg" />
        </div>
        <p id="cf7_controls">
            <span class="selected">Image 1</span>
            <span>Image 2</span>
            <span>Image 3</span>
            <span>Image 4</span>
        </p>
    </div>

I pulled this code from http://css3.bradshawenterprises.com/cfimg/#cfimg7

Unfortunately, it doesn't work right. A live demo of my WIP can be viewed on http://marc-with-a-c.com

The error in google chrome is: Uncaught ReferenceError: $ is not defined marc-with-a-c.com/:97

Thanks for helping!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You does not have jQuery library included in the page

Add the line to include jQuery before the script element:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
    $(document).ready(function() {

        $("#cf7_controls").on('click', 'span', function() {
            $("#cf7 img").removeClass("opaque");

            var newImage = $(this).index();

            $("#cf7 img").eq(newImage).addClass("opaque");
            $("#cf7_controls span").removeClass("selected");
            $(this).addClass("selected");
        });
    });
</script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...