简体   繁体   English

这个jQuery代码不起作用?

[英]this jquery code doesn't work?

I found this jsfiddle code , and I need it and I want to try that in my php file, but it doesn't work, whereas it's the same code, i just copied and paste and I don't change anything, but it still doesn't work. 我找到了这个jsfiddle 代码 ,我需要它,我想在我的php文件中尝试它,但是它不起作用,而它是相同的代码,我只是复制并粘贴了内容,但我什么都没做,但是仍然不起作用。

My Code: 我的代码:

<!DOCTYPE html>
<?php
?>
<html>
<head>
    <title></title>
    <script>
        $("#update").click(function() {
            $("#counter").html(function(i, val) {
                return val*1+1 
            });
        });
    </script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.jss"></script>
</head>
<body>
    <button id="update" type="button">Click Me</button>
    <div id="counter">10</div>
<?php

?>
</body>
</html>

Please show me my fault. 请告诉我我的错。 Any help would be appreciated! 任何帮助,将不胜感激!

You should really be able to debug this yourself. 您应该真的可以自己调试它。 Open your javascript console, and notice it says ReferenceError: $ is not defined . 打开您的JavaScript控制台,注意它显示ReferenceError: $ is not defined This means jquery isn't loaded. 这意味着没有加载jQuery。 Now look at the URL you put in your script-src. 现在查看您放入script-src中的URL。 Why does it end with in .jss ? 为什么以.jss You have a typo there. 你那里有错字。

If you correct that you'll still get the same error. 如果更正,您仍然会遇到相同的错误。 Why? 为什么? Because you use jquery before including it. 因为您在包含它之前使用了jquery。 So put the included jquery library before the custom code. 因此,将包含的jquery库放在自定义代码之前

Now, it still won't work. 现在,它仍然无法正常工作。 Why? 为什么? Because you attach an event before the DOM is loaded; 因为您在加载DOM之前附加了事件; so when your script is processed, the button doesn't exist! 因此,在处理脚本时,该按钮不存在! So have a look at http://api.jquery.com/ready/ and you should know what to add, wrap your javascript inside $(function() {...}) and you're good to go 因此,看看http://api.jquery.com/ready/ ,您应该知道要添加的内容,将您的javascript包装在$(function() {...}) ,您就可以开始了

Maybe it's just a typo in jquery.min.jss 也许这只是jquery.min.jss的错字

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.jss"></script>

it must be jquery.min.js 它必须是jquery.min.js

Or you placed your javascript before the jquery reference. 或者您将javascript放在jQuery引用之前。

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $('#target').click(function() {
    $('#output').html(function(i, val) { return val*1+1 });
});
});
</script>
</head>
<body>

<button id="target" type="button">Click Me</button>
<div id="output">10</div>

</body>
</html>

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

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