繁体   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