我如何创建一个全局可访问的变量? [英] How do I create a globally accessible variable?
本文介绍了我如何创建一个全局可访问的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图让一个变量可以在任何地方工作,而不是在一个函数中。
我该如何做?我搜索了1小时,但找不到:($ / b>
我试过了这个
<$
$
noneTxt = document.innerHTML =Nothing is playing!;
函数plinf(){
if(myAudio.paused){
myAudio.play();
}
else {
myAudio.pause();
document.getElementById('message')。innerHTML = noneTxt;
}
}
但是只有当我用var前缀将函数放入函数时才有效。 ..
那么我怎样才能使它有点儿全球化?:/ b / $ c> window 对象附加你的全局变量,但不是一个很好的主意,可能导致与其他用法的冲突
window.noneTxt = document.innerHTML =Nothing is playing!;
I'm trying to make a variable thats can work anywhere, and just not in one function.
How do i do this? I've searched for like 1 hour now and i can't find it :(
I tried this
noneTxt = document.innerHTML="Nothing is playing!";
function plinf() {
if (myAudio.paused) {
myAudio.play();
}
else {
myAudio.pause();
document.getElementById('message').innerHTML=noneTxt;
}
}
But it only works if I put the variable inside the function with var prefix... So how can i make it kinda global? :/
解决方案
you can always use the window
object to attach your "global" variable, but not a very good idea, could cause collisions with other usages
window.noneTxt = document.innerHTML="Nothing is playing!";
这篇关于我如何创建一个全局可访问的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文