我有一个Javascript函数,并希望从函数外部检索id值。如何使“id”成为一个静态变量并从函数外部访问它。从函数外部访问变量?
<input type="text" name="editinput" id="editinputtext" placeholder="Type Contribution...">
<script>
$(".contributions").click(function(){
var id = this.id;
});
document.getElementById("editinputtext").value = id;
注意'id'将是'undefined'这里的时候,它分配给'value'和它看起来并不像是一个永远不会改变... – deceze
当我运行它,它说,该值是不确定的,虽然我尝试了console.log,它返回了“id”的值。 –
@Hadi是的,因为*执行'click'函数时*。你为什么不在''click''函数内分配'editinputtext' ***? – deceze