为什么我的JavaScript显然陷入了无限循环?

时间:2019-04-08 17:46:01

标签: javascript

我正在定义一个on'submit'事件,如下所示:

HTML代码是:

<div class="messages">
            <ul>
                <li class="sent">
                    <img src="/img/person1.png" alt="" />
                    <p>How the hell am I supposed to get a jury to believe you when I am not even sure that I do?!</p>
                </li>
                <li class="replies">
                    <img src="/img/botimg.png" alt="" />
                    <p>When you're backed against the wall, break the god damn thing down.</p>
                </li>
                <li class="replies">
                    <img src="/img/botimg.png" alt="" />
                    <p>Excuses don't win championships.</p>
                </li>
                <li class="sent">
                    <img src="/img/person1.png" alt="" />
                    <p>Oh yeah, did Michael Jordan tell you that?</p>
                </li>
                <li class="replies">
                    <img src="/img/botimg.png" alt="" />
                    <p>No, I told him that.</p>
                </li>
            </ul>
        </div>
        <div class="message-input">
            <div class="wrap">
            <input type="text" placeholder="Write your message..." />
            <i class="fa fa-paperclip attachment" aria-hidden="true"></i>
            <button class="submit"><i class="fa fa-paper-plane" aria-hidden="true"></i></button>
            </div>
        </div>

辅助功能:

function newMessage() {
    console.log("Inside newMessage");
    message = $(".message-input input").val();
    if($.trim(message) == '') {
        return false;
    }
    $('<li class="sent"><img src="/img/person1.png" alt="" /><p>' + message + '</p></li>').appendTo($('.messages ul'));
    $('.message-input input').val(null);
    $(".messages").animate({ scrollTop: $(document).height() }, "fast");
};

提交点击功能是:

$('.submit').click(function() {

  newMessage();

});

现在,每当我单击“提交”按钮时,“ Inside newMessage”消息就会不断在JavaScript控制台中记录。

1 个答案:

答案 0 :(得分:0)

在清除Chrome浏览器的历史记录,特别是“ cookie和其他数据”后,它现在可以正常工作。