jquery 点击无反应,请问哪里出错了?

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>随机名言</title>
    <link rel="stylesheet" href="index.css">
    <script>
    $(document).ready(function() {
        $(".btn-new").on("click", function() {
            $(".quote").html("Here is the message");
        });
    });
    </script>
</head>

<body>
    <main class="qtbox">
        <section class="qtbox-change">
            <q class="quote">
                Soylent Green is people! Soylent Green is people! Soylent Green is people! Soylent Green is people!
            </q>
            <p class="author">-- 陈思</p>
        </section>
        <section class="qtbox-btngroup">
            <button class="btn btn-qq"></button>
            <button class="btn btn-weibo"></button>
            <button class="btn btn-new">New quote</button>
        </section>
    </main>

    <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous">
    </script>
</body>

</html>

css

html {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

body {
    display: flex;
    justify-content: center;
    background-color: #000;
}

.qtbox {
    width: 600px;
    height: 130px;
    padding: 25px 50px;
    background-color: #fff;
    border-radius: 2.5%;
}

.author {
    text-align: right;
}


.qtbox-btngroup {
    position: relative;
}

.qtbox-btngroup {
    width: 83%;
    margin-left: 8.5%;
    margin-right: 8.5%;
}

.qtbox-btngroup .btn {
    font-size: 16px;
    color: #fff;
    background-color: #000;
    height: 38px;
    border: 0;
    cursor: pointer;
    transition: background-color 1s ease-in;
}

.qtbox-btngroup .btn:focus {
    outline: none;
}

.qtbox-btngroup .btn:hover {
    opacity: 0.7;
}

.qtbox-btngroup .btn-new {
    font-family: Arial, Verdana, Sans-serif;
    position: absolute;
    right: 0;
    border-radius: 3px;
}

.qtbox-btngroup .btn-weibo,
.qtbox-btngroup .btn-qq {
    width: 40px;
    border-radius: 100%;
}

.qtbox-btngroup .btn-qq {
    background: url("images/index-qq.png") center center no-repeat #000;
}

.qtbox-btngroup .btn-weibo {
    background: url("images/index-weibo.png") center center no-repeat #000;
}
阅读 5.5k
9 个回答

请把头部的script标签移到jquery后面

    <script>
    $(document).ready(function() {
        $(".btn-new").on("click", function() {
            $(".quote").html("Here is the message");
        });
    });
    </script>

这段放在加载jQuery的<script>的后面,不然你的jQuery都没加载进来,怎么能使用它的方法呢

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>随机名言</title>
    
</head>

<body>
    <main class="qtbox">
        <section class="qtbox-change">
            <q class="quote">
                Soylent Green is people! Soylent Green is people! Soylent Green is people! Soylent Green is people!
            </q>
            <p class="author">-- 陈思</p>
        </section>
        <section class="qtbox-btngroup">
            <button class="btn btn-qq"></button>
            <button class="btn btn-weibo"></button>
            <button class="btn btn-new">New quote</button>
        </section>
    </main>

    <script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous">
    </script>
    <script>
        $(document).ready(function() {
            $(".btn-new").on("click", function() {
                $(".quote").html("Here is the message");
            });
        });
        </script>
</body>

</html>

顺序错了 jq引入放太下面了 要先引入jq再写你的业务代码

你要先加载jQuery,然后再使用jQuery,才有效果

解决方案:

// 先加载jQuery
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous">
    </script>
 // 再使用
<script>
    $(document).ready(function() {
        $(".btn-new").on("click", function() {
            $(".quote").html("Here is the message");
        });
    });
    </script>
 

F12打开控制条看什么错误,如果是表示获取元素未定义,那么大概率是因为页面元素未加载完造成的。jq可以改成window.onload这个是加载完所有元素后才运行。不是jq文件放不放后面的问题,我JQ不知道,反正JS我从来都是放最后

你把head里面的JS代码放到jquery文件后面试试,少年

新手上路,请多包涵

你把上面的script放到下面载入jq文件的下面,文档加载的顺序是由上而下的,只有加载了jq文件,才能引用jq里面的方法。

新手上路,请多包涵

1.先查看引入顺序:js引入路径问题,jq的使用必须先引入jq.js 的类库。
2.查看选择器的正确还是作物
3.$("p").on("click",function(){

alert(1);//检查点击事件

})
4.查看控制台报错信息,根据提示进行修改

引用的jquery应该在前面,脚本还是应该放页面底部出于页面性能的考虑

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题