JS 函数给全局变量赋值

JavaScript015

JS 函数给全局变量赋值,第1张

你的意思是把局部变量的值赋给全局变量吧我举个例子吧:<input type=“button” value=“1” onclick="MicroNet_OnSubmit('bbb')" ><input type=”button“ value=”2“ onclick="MicroNet_OnSubmit1()" ><script>var liuyin="aaa"function MicroNet_OnSubmit(flvFile) { liuyin= flvFile}function MicroNet_OnSubmit1() {alert(liuyin)} </script>上面的代码放在htm文件里,先点击按钮1再点击按钮2,看看值有没有变

<head>

<script type="text/javascript">

function test(str){

    document.getElementById("tt").value = str

}

</script>

</head>

<body>

<button onclick="test('abc')"></button>

<button onclick="test('qwe')"></button>

<button onclick="test('zha')"></button>

<input type="text" id="tt" />

</body>

这种方式也能达到你想要的效果,其实可以不用使用全局变量,是需求中必须使用全局变量?