jQuery不起作用,关于我如何包含它? [重复]

问题描述 投票:1回答:1

这个问题在这里已有答案:

我对这个社区以及一般的jQuery / JavaScript都是新手,但我只是花了一个小时研究,我根本看不出什么是错的。

我试图说明,当你点击它确定它改变了它的类。

我的代码的重要部分是

HTML:

<div class="taskbar">
        <div id="taskbar-start" class="taskbar-start-inactive">
            <img class="taskbar-start-logo" src="img/logo.png" /> 
            <div class="taskbar-start-text">Start</div>
        </div>
</div>

CSS:

#taskbar-start {
    margin-top: 4px;
    margin-bottom: 2px;
    margin-left: 2px;
    width: 90px;
    height: 33px;
    background-color: #C2C5CA;
    cursor: pointer;
}
.taskbar-start-inactive {
    box-shadow: -1px -1px 0px 0px #000 inset, 1px 1px 0px 0px #FFF inset, -2px -2px 0px 0px #868A8E inset;
}
.taskbar-start-active {
    box-shadow: -1px -1px 0px 0px #FFF inset, 1px 1px 0px 0px #000 inset, 2px 2px 0px 0px #868A8E inset;
}

JavaScript的

$('#taskbar-start').click(function() {
    $(this).toggleClass('taskbar-start-active');
    alert("Yeah!");
});

这不起作用。现在,我不想下载jQuery来使用它,因为一旦完成,我想在Tumblr上使用代码作为主题,在那里下载jQuery不会很好用我假设。目前我拥有它,我的JavaScript文件包含如下:

<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script src="javascript.js"></script>

我尝试了一个我在其他地方找到的代码来测试我的jQuery是否已加载(我忘了我发现它的地方,我很抱歉),这是

window.onload = function() {
if (window.jQuery) {  
    // jQuery is loaded  
    alert("Yeah!");
} else {
    // jQuery is not loaded
    alert("Doesn't Work");
}}

这似乎有效,但我的代码没有。如果我在这里复制我的整个代码,然后尝试一下,它就可以了,所以它可能是试图包含jQuery的东西?

就像我说的那样,我是超级新人,所以如果我的问题以某种方式变得愚蠢,我真的很抱歉。

javascript jquery html css
1个回答
1
投票

把你的代码放在document.ready里面

<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script src="javascript.js"></script>
<script>
    $(document).ready(function(){
        $('#taskbar-start').click(function() {
            $(this).toggleClass('taskbar-start-active');
            alert("Yeah!");
        });
    });
</script>
© www.soinside.com 2019 - 2024. All rights reserved.