簡體   English   中英

如何在inner.html中調用函數

[英]How to call function inside the inner.html

這是我在JS文件中的正常功能。 我需要closeHomeScreenPopupdocument.getElementById("addToHomeScreenPrompt").innerHTML內部定義的closeHomeScreenPopup函數。 如您所見,我已經將onclick作為字符串傳遞了。

export default function register() {
  // Checks if should display install popup notification:
  if (isIos() && !isInStandaloneMode()) {
    function closeHomeScreenPopup() {
      console.log("jkjilkjlkjlkjlkjlkjlk");
    }
    document.getElementById("addToHomeScreenPrompt").innerHTML =
      '<div onclick="closeHomeScreenPopup()"><span class="plush--sign">+</span><p>Install the webapp on your iPhone:tap <img style="width: 20px; margin: 0 4px;" alt="" src="img/share_icon.png"/> and then Add to homescreen.</p></div>';
  }
}

但是在div onclick時出現錯誤

Uncaught ReferenceError: closeHomeScreenPopup is not defined
    at HTMLDivElement.onclick (temperature-schedule?roomId=2:51)

我怎樣才能調用closeHomeScreenPopup從內inner.html

JS中的所有內容都必須包含作用域。 因此,如果直接在寄存器函數中定義函數,則該函數將不會綁定到窗口對象。

closeHomeScreenPopup函數應全局定義

 window[closeHomeScreenPopup] = function() {
      console.log("jkjilkjlkjlkjlkjlkjlk");
    }

 function closeHomeScreenPopup() { console.log("jkjilkjlkjlkjlkjlkjlk"); } document.getElementById("addToHomeScreenPrompt").innerHTML = '<div onclick="closeHomeScreenPopup()"><span class="plush--sign">+</span><p>Install the webapp on your iPhone:tap <img style="width: 20px; margin: 0 4px;" alt="" src="img/share_icon.png"/> and then Add to homescreen.</p></div>'; 
 <div id="addToHomeScreenPrompt"></div> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM