Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery JavaScript自动从聊天系统$(元素)[0]Vs$(元素)滚动到底部_Javascript_Jquery - Fatal编程技术网

jQuery JavaScript自动从聊天系统$(元素)[0]Vs$(元素)滚动到底部

jQuery JavaScript自动从聊天系统$(元素)[0]Vs$(元素)滚动到底部,javascript,jquery,Javascript,Jquery,此代码使滚动条在您键入消息时自动向下移动 但是, 如果在最后删除[0],滚动条不会移动,但内容会添加到聊天窗口中 这有什么区别 this.$chatHistory.scrollTop(this.$chatHistory.scrollHeight); scrollHeight是一个DOM属性,而不是jQuery属性。[0]正在从jQuery结果集中获取DOM元素。如果没有[0],您将访问jQuery结果集的scrollHeight属性(未定义),然后将未定义传递给jQuery scrollTop

此代码使滚动条在您键入消息时自动向下移动

但是,

如果在最后删除[0],滚动条不会移动,但内容会添加到聊天窗口中

这有什么区别

this.$chatHistory.scrollTop(this.$chatHistory.scrollHeight);
scrollHeight是一个DOM属性,而不是jQuery属性。[0]正在从jQuery结果集中获取DOM元素。如果没有[0],您将访问jQuery结果集的scrollHeight属性(未定义),然后将未定义传递给jQuery scrollTop方法

this.$chatHistory.scrollTop(this.$chatHistory.scrollHeight);
(function(){

  var chat = {
    messageToSend: '',
    messageResponses: [
      'Why did the web developer leave the restaurant? Because of the table layout.',
      'How do you comfort a JavaScript bug? You console it.',
      'An SQL query enters a bar, approaches two tables and asks: "May I join you?"',
      'What is the most used language in programming? Profanity.',
      'What is the object-oriented way to become wealthy? Inheritance.',
      'An SEO expert walks into a bar, bars, pub, tavern, public house, Irish pub, drinks, beer, alcohol'
    ],
    init: function() {
      this.cacheDOM();
      this.bindEvents();
      this.render();
    },
    cacheDOM: function() {
      this.$chatHistory = $('.chat-history');
      this.$button = $('button');
      this.$textarea = $('#message-to-send');
      this.$chatHistoryList =  this.$chatHistory.find('ul');
    },
    bindEvents: function() {
      this.$button.on('click', this.addMessage.bind(this));
      this.$textarea.on('keyup', this.addMessageEnter.bind(this));
    },
    render: function() {
      this.scrollToBottom();
      if (this.messageToSend.trim() !== '') {
        var template = Handlebars.compile( $("#message-template").html());
        var context = { 
          messageOutput: this.messageToSend,
          time: this.getCurrentTime()
        };

        this.$chatHistoryList.append(template(context));
        this.scrollToBottom();
        this.$textarea.val('');

        // responses
        var templateResponse = Handlebars.compile( $("#message-response-template").html());
        var contextResponse = { 
          response: this.getRandomItem(this.messageResponses),
          time: this.getCurrentTime()
        };

        setTimeout(function() {
          this.$chatHistoryList.append(templateResponse(contextResponse));
          this.scrollToBottom();
        }.bind(this), 1500);

      }

    },

    addMessage: function() {
      this.messageToSend = this.$textarea.val()
      this.render();         
    },
    addMessageEnter: function(event) {
        // enter was pressed
        if (event.keyCode === 13) {
          this.addMessage();
        }
    },
    scrollToBottom: function() {
       this.$chatHistory.scrollTop(this.$chatHistory[0].scrollHeight);
    },
    getCurrentTime: function() {
      return new Date().toLocaleTimeString().
              replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3");
    },
    getRandomItem: function(arr) {
      return arr[Math.floor(Math.random()*arr.length)];
    }

  };

  chat.init();

  var searchFilter = {
    options: { valueNames: ['name'] },
    init: function() {
      var userList = new List('people-list', this.options);
      var noItems = $('<li id="no-items-found">No items found</li>');

      userList.on('updated', function(list) {
        if (list.matchingItems.length === 0) {
          $(list.list).append(noItems);
        } else {
          noItems.detach();
        }
      });
    }
  };

  searchFilter.init();

})();