這裏是我的代碼:jQuery的定義點擊功能將覆蓋所有的點擊功能
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" ></script>
<script type="text/javascript" >
$(document).ready(function() {
var qtypes = ['Text','Area'];
for(qt in qtypes){
console.log('#submitQuestion' + qtypes[qt] + ' ' + qt);
var name = '#submitQuestion' + qtypes[qt];
$(name).click(function(){
console.log('clicked ' + name);
});
$('#submitQuestion' + qtypes[0]).click();
}
$('#submitQuestion' + qtypes[0]).click();
$('#submitQuestion' + qtypes[1]).click();
});
</script>
<input type="button" id="submitQuestionText" value="submit Question" />
<input type="button" id="submitQuestionArea" value="submit Area" />
結果在控制檯輸出:
#submitQuestionText 0
clicked #submitQuestionText
#submitQuestionArea 1
clicked #submitQuestionArea
clicked #submitQuestionArea
clicked #submitQuestionArea
爲什麼使用for循環覆蓋所有點擊()函數?我希望看到輸出的最後兩行是:
clicked #submitQuestionText
clicked #submitQuestionArea
jQuery click()的文檔聲明它與真正的點擊事件不完全相同。 – Dave 2012-02-24 20:11:50
這是一個示波器問題,試試這個問題的答案:http://stackoverflow.com/questions/2192348/closures-in-a-for-loop – 2012-02-24 20:15:04