简体   繁体   English

jQuery代码可在测试中工作,但不在现场

[英]jQuery code working in testing but not on site

So I have the following code. 所以我有以下代码。

<script src="http://jamesleist.com/wp-content/uploads/2013/01/js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script>
    $(document).ready(function() {
        $('.me').hide();
        $('.clickme').click(function() {
            $(this).next('.me').animate({
                height: 'toggle'
            }, 500);
        });
    });
</script>    

<div class="clickme" style="background-color: #333333; color: #FFFFFF; padding: 10px; width: 200px; cursor:pointer;">
  Click here to toggle me in and out =)
</div>
<img class="me" src="http://www.randomsnippets.com/wp-content/uploads/2011/04/2.png" alt="It&#039;s me....ah!!!" title="Allen Liu" width="100" height="77" class="alignnone size-full wp-image-552" style="border: none;" />

<div class="clickme" style="background-color: #333333; color: #FFFFFF; padding: 10px; width: 200px; cursor:pointer;">
  Click here to toggle me in and out =)
</div>
<img class="me" src="http://www.randomsnippets.com/wp-content/uploads/2011/04/2.png" alt="It&#039;s me....ah!!!" title="Allen Liu" width="100" height="77" class="alignnone size-full wp-image-552" style="border: none;" />

It works fine when running on its own, however when I implement this into the main site it does not work. 单独运行时,它可以正常工作,但是当我将其实现到主站点中时,它将无法工作。

You can see where I have tried to implement it at the following URL. 您可以在以下URL上查看我在哪里尝试实现它。

http://jamesleist.com/portfolio http://jamesleist.com/portfolio

It is really confusing me :-( I am using Wordpress by the way and jQuery is linked to in the header.php file. 这真的让我感到困惑:-(我正在使用Wordpress,并且jQuery已链接到header.php文件中。

I am assuming this is a problem with Wordpress? 我假设这是Wordpress的问题?

Many thanks in advance for your help. 在此先感谢您的帮助。

.me is not the next element, the next element is a paragraph containing the image ? .me不是下一个元素,下一个元素是包含图像的段落?

$(document).ready(function() {
    $('.me').hide();
    $('.clickme').on('click', function() {
        $(this).next('p').find('.me').animate({
            height: 'toggle'
        }, 500);
    });
});

Your img tags have two class attributes defined. 您的img标记定义了两个class属性。 I'm not sure if this is 100% the cause, but that should definitely be fixed. 我不确定这是否是100%的原因,但是那绝对应该解决。

Like this: 像这样:

<img class="me alignnone size-full wp-image-552" src="http://www.randomsnippets.com/wp-content/uploads/2011/04/2.png" alt="It&#039;s me....ah!!!" title="Allen Liu" width="100" height="77" style="border: none;" />

Your images have 'class' defined twice, you can't do that. 您的图片有两次定义的“类别”,您不能这样做。 You need to include all your classes including the 'me' class inside one class declaration separated by spaces. 您需要将所有类(包括“ me”类)包括在一个由空格分隔的类声明中。 A simple run through a validator shows the errors pretty clearly. 通过验证程序的简单运行即可非常清楚地显示错误。

I would also consider moving your scripts both internal and external to the bottom of your page so that the dom isn't waiting on them to load. 我还考虑将您的脚本内部和外部都移动到页面底部,以使dom不在等待它们加载。 Moving all your inline styles into the stylesheet would be a good idea as well so your code is easier to read and debug. 将所有内联样式移动到样式表中也是一个好主意,因此您的代码更易于阅读和调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM