Javascript 将新内容添加到div时滚动到底部

Javascript 将新内容添加到div时滚动到底部,javascript,jquery,var,scrolltop,Javascript,Jquery,Var,Scrolltop,当新内容发送到聊天室时,如何使聊天室滚动到底?我试图添加如下内容: $('#what do I have to put here?').animate({scrollTop: $('#and here?').prop("scrollHeight")}, 200); 但是我不能让它工作(我不知道在代码中的什么地方放置它) 谢谢 这是代码的重要部分。。。也许我必须在这里添加它(但我不知道如何或在哪里添加): var_answerBot=新的answerBot(); 功能键按输入(发送器、事件){

当新内容发送到聊天室时,如何使聊天室滚动到底?我试图添加如下内容:

$('#what do I have to put here?').animate({scrollTop: $('#and here?').prop("scrollHeight")}, 200);
但是我不能让它工作(我不知道在代码中的什么地方放置它)

谢谢

这是代码的重要部分。。。也许我必须在这里添加它(但我不知道如何或在哪里添加):


var_answerBot=新的answerBot();
功能键按输入(发送器、事件){
if(event.which==13){
document.getElementById('subcent').innerHTML+=\u answerBot.processInput(sender.value);
sender.value='';
}
}
这是一个单独的scripts.js文件:

var answerBot = function () {
    var _this = this;
    _this.processInput = function (text) {
        updateUrl(text);
        var _result = "<p class='answerbot-input'>" + text + "</p>";
        text = text.replace(new RegExp("[ ]{2,}", "g"), " ");
        var _words = text.toLowerCase().split(" ");
        var _answers = [];
        var _title = "";
        if (_words.length === 0 || _words.toString() === '') { //if the input is empty
            _answers = _this.specialContext.emptyInput;
            _title = _this.specialContext.emptyInput;
        } else {
            var _possibleAnswers = findMatches(_words);
            if (_possibleAnswers.length === 0) { //if no answer found
                _answers = _this.specialContext.wrongInput;
                _title = _this.specialContext.wrongInput;
            }
            if (_possibleAnswers.length == 1) { //context recognized
                _answers = _this.answers[_possibleAnswers[0]].values;
                _title = _this.answers[_possibleAnswers[0]].description;
            }
            if (_possibleAnswers.length > 1) {
                _result += formatText(_this.specialContext.rephrase, _this.specialContext.rephrase);
                for (var i = 0; i < _possibleAnswers.length; i++) {
                    _result += formatText(_this.answers[_possibleAnswers[i]].description, _this.answers[_possibleAnswers[i]].description);
                }
            }
        }
        if (_answers.length > 0) {
            var _rand = Math.floor((Math.random() - 0.001) * _answers.length);
            _result += formatText(_answers[_rand], _title);
        }
        return _result;
    };

    function formatText(text, title) {
        return "<p class=\'answerbot-ai\' title=\'" + title + "\'>" + text + "</p>";
    }

    function findMatches(words) {
        var foundKeywords = [];
        var _possibleAnswers = [];
        for (var i = 0; i < _this.keywords.length; i++) {
            foundKeywords[i] = 0;
            for (var j = 0; j < words.length; j++) {
                if (_this.keywords[i].keys.indexOf(words[j]) >= 0) {
                    foundKeywords[i]++;
                    if (foundKeywords[i] == _this.keywords[i].keys.length) {
                        return [_this.keywords[i].value];
                    }
                }
            }
            if (foundKeywords[i] * 2 > _this.keywords[i].keys.length) {
                _possibleAnswers.push(_this.keywords[i].value);

            }
        }
        return _possibleAnswers.filter(function (elem, pos) {
            return _possibleAnswers.indexOf(elem) == pos;
        });
    }

    function updateUrl(text){
        history.pushState(null, null, "#question=" + encodeURIComponent(text));
        if(typeof ga === "function")//google analytics
            ga('send', 'event', 'question', text);
    }
};
var answerBot=函数(){
var_this=这个;
_this.processInput=函数(文本){
更新(文本);
var_result=“

”+text+“

”; text=text.replace(newregexp(“[]{2,}”,“g”),”); var_words=text.toLowerCase().split(“”); var _answers=[]; var _title=“”; 如果(_words.length==0 | |_words.toString()===''){//如果输入为空 _answers=\u this.specialContext.emptyInput; _title=\u this.specialContext.emptyInput; }否则{ var _possibleAnswers=查找匹配项(_单词); 如果(_possibleAnswers.length==0){//如果找不到答案 _answers=\u this.specialContext.ErrorInput; _title=\u this.specialContext.ErrorInput; } 如果(_possibleAnswers.length==1){//上下文已识别 _答案=_this.answers[_possibleAnswers[0]]值; _title=\u this.answers[\u possibleAnswers[0]]。描述; } 如果(_possibleAnswers.length>1){ _结果+=格式文本(_this.specialContext.repranse,_this.specialContext.repranse); 对于(变量i=0;i<_possibleAnswers.length;i++){ _结果+=格式文本(_this.answers[_possibleAnswers[i]]说明,_this.answers[_possibleAnswers[i]]说明); } } } 如果(_answers.length>0){ var _rand=Math.floor((Math.random()-0.001)*\u answers.length); _结果+=格式文本(\u答案[\u兰德],\u标题); } 返回结果; }; 函数格式文本(文本、标题){ 返回“

”+text+“

”; } 函数查找匹配(字){ var-foundKeywords=[]; var _possibleAnswers=[]; 对于(var i=0;i<\u this.keywords.length;i++){ 关键词[i]=0; for(var j=0;j=0){ 关键词[i]++; if(foundKeywords[i]==\u this.keywords[i].keys.length){ 返回[_this.keywords[i].value]; } } } if(foundKeywords[i]*2>\u this.keywords[i].keys.length){ _可能的swers.push(_this.keywords[i].value); } } 返回_possibleAnswers.filter(函数(元素、位置){ 返回_possibleAnswers.indexOf(elem)=pos; }); } 函数updateUrl(文本){ history.pushState(null,null,#question=“+encodeURIComponent(text)); if(typeof ga==“function”)//谷歌分析 ga(“发送”、“事件”、“问题”、文本); } };
函数AddMessage(){
var$message=$(“”)文本(“消息”);
var$messages=$(“#messages”);
$messages.append($message);
$messages.animate({
scrollTop:$messages.prop(“scrollHeight”)
});
}
#消息{
边框:1px实心#000;
高度:100px;
溢出:自动;
}

添加消息
您是否检查了以下内容:
var answerBot = function () {
    var _this = this;
    _this.processInput = function (text) {
        updateUrl(text);
        var _result = "<p class='answerbot-input'>" + text + "</p>";
        text = text.replace(new RegExp("[ ]{2,}", "g"), " ");
        var _words = text.toLowerCase().split(" ");
        var _answers = [];
        var _title = "";
        if (_words.length === 0 || _words.toString() === '') { //if the input is empty
            _answers = _this.specialContext.emptyInput;
            _title = _this.specialContext.emptyInput;
        } else {
            var _possibleAnswers = findMatches(_words);
            if (_possibleAnswers.length === 0) { //if no answer found
                _answers = _this.specialContext.wrongInput;
                _title = _this.specialContext.wrongInput;
            }
            if (_possibleAnswers.length == 1) { //context recognized
                _answers = _this.answers[_possibleAnswers[0]].values;
                _title = _this.answers[_possibleAnswers[0]].description;
            }
            if (_possibleAnswers.length > 1) {
                _result += formatText(_this.specialContext.rephrase, _this.specialContext.rephrase);
                for (var i = 0; i < _possibleAnswers.length; i++) {
                    _result += formatText(_this.answers[_possibleAnswers[i]].description, _this.answers[_possibleAnswers[i]].description);
                }
            }
        }
        if (_answers.length > 0) {
            var _rand = Math.floor((Math.random() - 0.001) * _answers.length);
            _result += formatText(_answers[_rand], _title);
        }
        return _result;
    };

    function formatText(text, title) {
        return "<p class=\'answerbot-ai\' title=\'" + title + "\'>" + text + "</p>";
    }

    function findMatches(words) {
        var foundKeywords = [];
        var _possibleAnswers = [];
        for (var i = 0; i < _this.keywords.length; i++) {
            foundKeywords[i] = 0;
            for (var j = 0; j < words.length; j++) {
                if (_this.keywords[i].keys.indexOf(words[j]) >= 0) {
                    foundKeywords[i]++;
                    if (foundKeywords[i] == _this.keywords[i].keys.length) {
                        return [_this.keywords[i].value];
                    }
                }
            }
            if (foundKeywords[i] * 2 > _this.keywords[i].keys.length) {
                _possibleAnswers.push(_this.keywords[i].value);

            }
        }
        return _possibleAnswers.filter(function (elem, pos) {
            return _possibleAnswers.indexOf(elem) == pos;
        });
    }

    function updateUrl(text){
        history.pushState(null, null, "#question=" + encodeURIComponent(text));
        if(typeof ga === "function")//google analytics
            ga('send', 'event', 'question', text);
    }
};