代码适用于JSFiddle,但在本地保存时不起作用 [英] Code works on JSFiddle but when saved locally doesn't work

查看:91
本文介绍了代码适用于JSFiddle,但在本地保存时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试从用户那里接收输入并将其显示为列表。幸好有人已经做到了,并在JSFiddle上找到了它。代码工作正常,但当我在本地移动时,它不起作用。当我输入文字时,没有任何反应。我在这里遇到类似的问题,但仍然无法正常工作。

I am trying to take in input from the user and display it as a list. Thankfully someone has already done it and found it on JSFiddle. The codes works fine there but when I move it locally, it is not working. When I enter the text, nothing happens. I went through similar issues here but still not working.

如果您发现错误,请指教。谢谢。

Please advice if you spot the mistake. Thank you.

<!DOCTYPE HTML>
<html>
    <head>     
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css" />
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>

        <title>List maker</title>
        <style>
            a {
                margin-left: 20px;
                color: maroon;
                text-decoration: none;
                font-weight: bold;
            }
        </style>

        <script>
            $('form').submit(function () {
                if ($('input').val() !== '') {
                    var input_value = $('input').val();
                    $('ul').append('<li>' + input_value + '<a href="">x</a></li>');
            };
                $('input').val('');
                return true;
            });

            $(document).on('click', 'a', function (e) {
                e.preventDefault();
                $(this).parent().remove();
            });
        </script>
    </head>

    <body>
        <form>
            <label>Enter info and press enter</label>
            <input type="text" />
        </form>
        <ul></ul>
    </body>
</html> 


推荐答案

将它放在 $(document).ready(){} 或将它移动到 body 的结尾处让它再次运作。

Since your jquery code comes before your html, i think putting it in a $(document).ready() {} or moving it to the end of the body will make it work again.

这篇关于代码适用于JSFiddle,但在本地保存时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆