jquery code works on codepen/jsfiddle but not html page

拈花ヽ惹草 提交于 2019-12-31 06:52:09

问题


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. 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.


回答1:


You need to wrap your code inside ready handler:

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

Or,

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


来源:https://stackoverflow.com/questions/24667006/jquery-code-works-on-codepen-jsfiddle-but-not-html-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!