需要帮助让这个字符串的Javascript工作

Need helping getting this string of Javascript to work

本文关键字:字符串 Javascript 工作 帮助      更新时间:2023-09-26

以下是我正在使用的内容:

  <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>

我从中提取了此代码http://css3.bradshawenterprises.com/cfimg/#cfimg7

不幸的是,它不能正常工作。我的在制品的实时演示可以在http://marc-with-a-c.com

谷歌chrome中的错误是:未捕获引用错误:$未定义marc-with-a-c.com/:97

谢谢你的帮助!

页面中没有包含jQuery库

在脚本元素之前添加一行以包含jQuery:

<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>

U需要包含jquery

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 

这实际上是一个使用jquery编写的代码。您必须将代码引用到jquery框架中,如:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

或者你可能想在他们的网站中下载该框架的缩小版

www.jquery.com

并参考您的代码

<script type = "text/javascript" src = "changeThisToThePathWhereYouSaveTheDownloadedJqueryFramework"></script>
<script type = "text/javascript">
    //your actual code here
</script>