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