天天看點

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 {} .

轉載于:https://www.cnblogs.com/chentianwei/p/9935543.html

繼續閱讀