简体   繁体   English

jQuery代码可在Codepen / jsfiddle上使用,但不能在html页面上使用

[英]jquery code works on codepen/jsfiddle but not html page

My code for collapsible jquery div works on codepen and JSFiddle , but when I copy everything to my page (content adapted), the div does not expand. 我的可折叠jquery div代码适用于codepenJSFiddle ,但是当我将所有内容复制到页面(内容已适应)时,div不会扩展。 What am I missing, or where did I go wrong? 我缺少什么,或者我哪里出了问题?

My script tags in the head are as follows: 我的脚本标签在头部如下:

<head>
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script> 

    <script type="text/javascript">
        $(".header").click(function () {
            $header = $(this);
            $content = $header.next();
            $content.slideToggle(500, function () {
                $header.text(function () {
                    return $content.is(":visible") ? "Collapse" : "Expand";
                });
            });
        });
    </script>
</head>

And my content within the body: 而我体内的内容:

<form method="post" action="sales_report.php" enctype="multipart/form-data">
    //multiple <input /> fields here
    <div class="container">
        <div class="header"><span>+Options</span></div>
        <div class="content">
            <h3>Designate the Column of:</h3>
            <p>Account Name</p>
            <input required type="number" name="sales_report_col_acc" id="sales_report_col_acc" value='1' />
            <p>Sale Amount</p>
            <input required type="number" name="sales_report_col_amt" id="sales_report_col_amt" value='2' />
            <p>Sale Date (optional)</p>
            <input type="number" name="sales_report_col_date" id="sales_report_col_date" value='3' />
            </br /> 
        </div>
    </div>
    <input type="submit" name="sales_report_submit" value="Go" />
</form>

Please advise on how to fix this? 请告知如何解决此问题? Am I missing something crucial that goes along with jquery. 我是否错过了与jquery一起使用的重要工具。

You need to wrap your code inside ready handler: 您需要将代码包装在ready处理程序中:

$(document).ready(function(){
     //your code here
});

Or, 要么,

$(function(){
   //your code here
});

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

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