<input type="number" id="num"/>
<script>
(function(window,document){
var num=document.querySelector("#num");
})(window,document);
</script>
var num=document.querySelector("#num");
上面这句代码获取的num是字符串还是数字呢?
如果 num.value原先是数字12,执行完num.value+1;
这句之后num输入框是字符串121还是数字13呢?
答案最终是121,如果想出现13,可以有两种办法:
-
var now=num.value-0; //把now 转为数字格式
num.value=now+1; -
num.value=++num.value;