【发布时间】:2026-02-14 00:10:02
【问题描述】:
为什么surveyName的值没有变化?
<script type = 'text/javascript'>
surveyName = "catNull";
function test(){
window['surveyName'] = "catTest";
}
</script>
</head>
<body>
<input onclick = 'test()' id = 'cat' class = 'test' type = 'button' value = 'category' />
<script>
document.write('<input id = "survey" class = "test" type = "button" value = "'+surveyName+'" />');
</script>
【问题讨论】:
-
在你点击按钮之前它不会改变。
-
如果网页中的变量发生了变化,但没有人看到它,它真的改变了吗?
-
SLaks,你的意思是,JavaScript 版本的薛定谔猫? :)
-
这里会发生的是一个按钮将被写入一个值“catNull”,当您单击它时,surveyName 变量会发生变化,但这不会影响输入按钮的值。为此,您需要类似 document.getElementById('survey').value = "catTest"; 的东西。 JavaScript 变量不会以这种方式自动绑定到 DOM。
标签: javascript events variables global local