带有innerhtml的全局变量

时间:2018-04-19 09:34:05

标签: javascript

我使用oninput事件触发器执行该函数,并且js位于页面底部的外部文件中。

如果我在函数内部移动变量,它是否可以放在外面?



var globalVariable = document.getElementById("div-tag").innerHTML;

function myFunc() {
    var amount = document.getElementById("txtValue").value;
    globalVariable = (amount * 0.10);
    
    console.log(globalVariable)
}

<input id="txtValue" oninput="myFunc()">
<div id="div-tag">0</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

这样就可以了解

&#13;
&#13;
var globalVariable = document.getElementById("div-tag");

function myFunc() {
    var amount = document.getElementById("txtValue").value;
    globalVariable.innerHTML = (amount * 0.10);
}
&#13;
<input id="txtValue" oninput="myFunc()">
<div id="div-tag">0</div>
&#13;
&#13;
&#13;

Codepen

答案 1 :(得分:0)

&#13;
&#13;
var globalVariable = document.getElementById("div-tag");

function myFunc() {
    var amount = document.getElementById("txtValue");
  var res =   (globalVariable.innerHTML = (amount.value * 0.10))
  console.log(res);
}
&#13;
<input id="txtValue" oninput="myFunc()">
<div id="div-tag">0</div>
&#13;
&#13;
&#13;