innerHTML用法及错误:无法设置未定义或null引用的属性“innerHTML”解决


在使用ActionCable时,

app/assets/javascripts/channels/calladdresses.coffee:

App.calladdress = App.cable.subscriptions.create "CalladdressChannel",
  connected: ->
    # Called when the subscription is ready for use on the server

  disconnected: ->
    # Called when the subscription has been terminated by the server

  received: (data) ->
    console.log(data)
    document.getElementById("app").innerHTML = data.title;

提示在chrome控制台 innerHTML is null 报告❌。

这是因为此时DOM还没有加载完成。

解决方法:

使用:

    document.addEventListener "turbolinks:load", ->
      document.getElementById("app").innerHTML = data.title;

传统做法是window.onload = function {} .


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM