[英]How to call function inside the inner.html
这是我在JS
文件中的正常功能。 我需要closeHomeScreenPopup
在document.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.