简体   繁体   English

JQuery在JSFiddle上工作,但不在我的网站上。

[英]JQuery work on JSFiddle but not on my site.

I have this problem: I've written a JQuery script that works on JSFiddle but it doesn't work on my site . 我有这个问题:我编写了一个在JSFiddle上可以使用的JQuery脚本,但是在我的网站上不起作用。

Here is the code [JQUERY] 这是代码[JQUERY]

$("i#ch1").click(function () {
    $('div#change-bg').removeClass();
    //remove classes so it can set for the clicked one "fa fa-circle" and for the other "fa fa-circle-o"
    $('i#ch1').removeClass();
    $('i#ch2').removeClass();
    $('i#ch3').removeClass();
    $('i#ch4').removeClass();

    $('div#change-bg').addClass('bg1');
    //add classes

    $('i#ch1').addClass('fa fa-circle');
    $('i#ch2').addClass('fa fa-circle-o');
    $('i#ch3').addClass('fa fa-circle-o');
    $('i#ch4').addClass('fa fa-circle-o');

});




$("i#ch2").click(function () {
    $('div#change-bg').removeClass();
    $('div#change-bg').addClass('bg2');
    //remove classes
    $('i#ch1').removeClass();
    $('i#ch2').removeClass();
    $('i#ch3').removeClass();
    $('i#ch4').removeClass();
    //add classes
    $('i#ch1').addClass('fa fa-circle-o');
    $('i#ch2').addClass('fa fa-circle');
    $('i#ch3').addClass('fa fa-circle-o');
    $('i#ch4').addClass('fa fa-circle-o');
});




$("i#ch3").click(function () {
    $('div#change-bg').removeClass();
    $('div#change-bg').addClass('bg3');
    //remove classes
    $('i#ch1').removeClass();
    $('i#ch2').removeClass();
    $('i#ch3').removeClass();
    $('i#ch4').removeClass();
    //add classes
    $('i#ch1').addClass('fa fa-circle-o');
    $('i#ch2').addClass('fa fa-circle-o');
    $('i#ch3').addClass('fa fa-circle');
    $('i#ch4').addClass('fa fa-circle-o');
}); 



$("#ch4").click(function () {
    $('div#change-bg').removeClass();
    $('div#change-bg').addClass('bg4');
    //remove classes
    $('i#ch1').removeClass();
    $('i#ch2').removeClass();
    $('i#ch3').removeClass();
    $('i#ch4').removeClass();
    //add classes
    $('i#ch1').addClass('fa fa-circle-o');
    $('i#ch2').addClass('fa fa-circle-o');
    $('i#ch3').addClass('fa fa-circle-o');
    $('i#ch4').addClass('fa fa-circle');
});             


//fa fa-dot-circle-o on mouse over
//fa fa-circle-o on mouse out


$("i#ch1").mouseover(function () {
    $("i#ch1").removeClass();
    $("i#ch1").addClass('fa fa-dot-circle-o');
});
$("i#ch1").mouseout(function () {
    $("i#ch1").removeClass();
    $("i#ch1").addClass('fa fa-circle-o');
});



$("i#ch2").mouseover(function () {
    $("i#ch2").removeClass();
    $("i#ch2").addClass('fa fa-dot-circle-o');
});
$("i#ch2").mouseout(function () {
    $("i#ch2").removeClass();
    $("i#ch2").addClass('fa fa-circle-o');
});


$("i#ch3").mouseover(function () {
    $("i#ch3").removeClass();
    $("i#ch3").addClass('fa fa-dot-circle-o');
});
$("i#ch3").mouseout(function () {
    $("i#ch3").removeClass();
    $("i#ch3").addClass('fa fa-circle-o');
});


$("i#ch4").mouseover(function () {
    $("i#ch4").removeClass();
    $("i#ch4").addClass('fa fa-dot-circle-o');
});
$("i#ch4").mouseout(function () {
    $("i#ch4").removeClass();
    $("i#ch4").addClass('fa fa-circle-o');
});


//by Niccolò Agnoletti

[script_tags_in_html_head] [script_tags_in_html_head]

<!--scripts-->
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://www.niccow3.site11.com/V2/javate.js"></script>

[HTML] [HTML]

<div id="change-bg" class="bg1">
<center>
        <div id="bg-buttons">
            <i class ="fa fa-circle" id="ch1"> </i>
            <i class ="fa fa-circle-o" id="ch2"> </i>
            <i class ="fa fa-circle-o" id="ch3"> </i>
            <i class ="fa fa-circle-o" id="ch4"> </i>
        </div>
            </center>
</div>

[CSS] [CSS]

.bg1{
    width:640px;
    height:480px;
    background: url('http://lorempixel.com/640/480');
    }
.bg2{
    width:640px;
    height:480px;
    background: url('http://lorempixel.com/640/480/sports/');
    }
.bg3{
     width:640px;
    height:480px;
    background: url('http://lorempixel.com/640/480/sports/This-is-sporty/');
    }
.bg4{
    width:640px;
    height:480px;
    background: url('http://dummyimage.com/640x4:3');
    }


#bg-buttons{display: inline;}
#ch1, #ch2, #ch3,#ch4 {color: orange; background:black;}



And I've also another problem: The table that should be on the bottom is just under the div on the top. 而且我还有另一个问题:应该在底部的表在顶部的div之下。 Can someone help? 有人可以帮忙吗?

You should wrap your code in $(document).ready(function(){}) or $(function(){}); 您应该将代码包装在$(document).ready(function(){})$(function(){});

JSFiddle automatically sets the option onLoad , but you'll have to manually include it in your code to make it work. JSFiddle自动设置选项onLoad ,但是您必须手动将其包含在代码中以使其起作用。

The reason why your code might not be working is because you might be trying to access elements, even before they existed. 您的代码可能无法正常工作的原因是,您可能甚至在元素存在之前就尝试访问它们。

1. You import jquery twice: 1.您两次导入jquery

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Remove one of them as they are conflicting. 由于它们有冲突,请删除其中之一。

2. You should wrap all your javascript code like this: 2.您应该像这样包装所有的javascript代码:

$(function(){
    // Your js code here
};

This will cause the code to be executed after page is completely loaded. 这将导致页面完全加载后执行代码。 Otherwise you try to operate on incomplete DOM tree. 否则,您将尝试对不完整的DOM树进行操作。
$(document).ready() solution from other answers will do the same but mine is shorter and preferred way. 来自其他答案的$(document).ready()解决方案将执行相同操作,但我的方法更短且更可取。

Inside of your file http://www.niccow3.site11.com/V2/javate.js you need to make sure that your code is inside of jquery's document ready event. 在文件http://www.niccow3.site11.com/V2/javate.js中,您需要确保代码在jquery的文档就绪事件中。 You do it at the bottom of the file, but the other content needs it as well. 您可以在文件的底部进行此操作,但其他内容也需要它。 The reason the code works on jsFiddle and not on your site is that you more than likely selected the "onload" handler for the code section in the left hand panel (it is default). 该代码在jsFiddle上而不在您的网站上运行的原因是,您很可能在左侧面板中为代码部分选择了“ onload”处理程序(默认)。 However, omitting this on your site meant that the DOM wasn't ready when the jquery executed. 但是,在您的站点上省略此选项意味着在执行jquery时DOM尚未准备就绪。 When that happens, no results return for the selectors and jquery silently fails to assign the event handlers. 发生这种情况时,选择器将不会返回任何结果,并且jQuery静默地无法分配事件处理程序。

So this 所以这

$("i#ch1").click(function () {
$('div#change-bg').removeClass();
//remove classes so it can set for the clicked one "fa fa-circle" and for the other "fa fa-circle-o"
$('i#ch1').removeClass();
$('i#ch2').removeClass();
$('i#ch3').removeClass();
$('i#ch4').removeClass();

$('div#change-bg').addClass('bg1');
//add classes

$('i#ch1').addClass('fa fa-circle');
$('i#ch2').addClass('fa fa-circle-o');
$('i#ch3').addClass('fa fa-circle-o');
$('i#ch4').addClass('fa fa-circle-o');

});

should be this 应该是这个

$(function(){
$("i#ch1").click(function () {
$('div#change-bg').removeClass();
//remove classes so it can set for the clicked one "fa fa-circle" and for the other "fa fa-circle-o"
$('i#ch1').removeClass();
$('i#ch2').removeClass();
$('i#ch3').removeClass();
$('i#ch4').removeClass();

$('div#change-bg').addClass('bg1');
//add classes

$('i#ch1').addClass('fa fa-circle');
$('i#ch2').addClass('fa fa-circle-o');
$('i#ch3').addClass('fa fa-circle-o');
$('i#ch4').addClass('fa fa-circle-o');

});
});

And the same for the next dozen code blocks. 接下来的十二个代码块也是如此。

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

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