【问题标题】:jQuery ID & function [closed]jQuery ID 和函数 [关闭]
【发布时间】:2016-01-08 01:39:23
【问题描述】:

请帮助我。我有问题!

例如

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>
    </head>

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>


    <body>
        <input type="button" id="clickmenow" value="ADD" />
        <script type="text/javascript">
      $('#clickmenow').click(function() {
    window.alert("DO IT");
});          
      </script>      
    </body>
</html>

请告诉我为什么它不起作用?! 如果我这样做了:

<script type="text/javascript">
        $(document).ready(function(){
            $("clickmenow").click(function(){
                window.alert("SHOWTHAT");
            });
        });
        </script>

这行不通!有什么建议!

【问题讨论】:

  • 您忘记导入 jQuery。
  • 缺少 Jquery 库。
  • 只要确保在您的 &lt;/body&gt; 标签之前包含 &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"&gt;&lt;/script&gt;
  • 对于未来,请确保您说出问题所在,而不仅仅是“它不起作用”并问“此代码不起作用!!为什么?!”

标签: javascript jquery html


【解决方案1】:

问题来自于添加 jQuery 的移动版本。尝试在包含 jquery mobile 之前添加此脚本。

<script>
    $(document).bind('mobileinit',function(){
        $.mobile.changePage.defaults.changeHash = false;
        $.mobile.hashListeningEnabled = false;
        $.mobile.pushStateEnabled = false;
    });
</script>

完整代码如下:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>
    </head>

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
    $(document).bind('mobileinit',function(){
        $.mobile.changePage.defaults.changeHash = false;
        $.mobile.hashListeningEnabled = false;
        $.mobile.pushStateEnabled = false;
    });
</script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

    <body>
        <input type="button" id="clickmenow" value="ADD" />
        <script type="text/javascript">
            $('#clickmenow').click(function() {
                window.alert("DO IT");
            });          
      </script>      
    </body>
</html>

【讨论】:

  • 我补充说这是行不通的 :(
  • 你遇到了什么错误?
  • 你有css文件吗?
  • 是的,现在可以了!但为什么 ?怎么样?
  • 谢谢你,你是男人!真的
【解决方案2】:

所以我没有足够的声誉来评论帖子。但是在 safari 中进行调试的一个好方法是进入首选项,然后在最底部的首选项的高级部分中有一个复选框,上面写着“在菜单栏中显示开发菜单”。确保选中此框。

之后,您转到菜单栏的“开发”部分并选择选项显示错误控制台。这将显示您遇到的错误。

这是错误控制台的外观图片和错误示例。错误以红色文本显示。

The Error Console in Safari

关于您的代码的问题,实际上您没有包含指向 jQuery 库的链接。你可以通过几种方式做到这一点。之前建议的方式仅在您有互联网连接时才有效。如果您愿意,您可以从他们的网站下载 jQuery 库并将其保存到您的网站文件夹中。然后,您需要做的只是使用脚本标签链接到该文件,就像您链接任何其他外部 javascript 文件一样。

jQuery 下载页面: https://jquery.com/download/

例子:

<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../jquery-ui-1.11.4/jquery-ui.min.js"></script>
<script type="text/javascript" src="../js/blur.js"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-26
    • 2012-02-22
    • 2013-03-23
    • 2022-01-19
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多