編輯爲幾個人指出,我會平變化混合的onclick ...
我this question昨天,這人的方式,固定親切回答我的貼問題,但我不明白爲什麼$("#selector").click
methon可行,但onchange=(...)
不行。
所以,如果我有HTML,在父元素的DataTable cloumn頭,其中頭部的任何點擊觸發的DataTable排序過程:
<div>
<input type="checkbox" id="select_all" onchange="SelectAll(event)">
SELECT ALL
</div>
//This is hit only AFTER the sort routine from the parent element is fired.
function SelectAll(event) {
event.stopPropagation();
var boxes = document.querySelectorAll("[name*=\"include_\"]"); //All checkboxes prefixed "include_".
var chk = document.getElementById('select_all').checked;
for (var i = 0; i < boxes.length; i++) {
if(!boxes[i].disabled) {
boxes[i].checked = chk;
}
}
}
//This is hit BEFORE the sort routine from the parent element is fired.
$("#select_all").click(function(event){
event.stopPropagation()
});
}
第二種方法,父元素的click事件之前發射,作品對我而言,但兩者之間的根本區別是什麼?
你正在使用onChange事件,而不是onclick。在某些瀏覽器中,當元素失去焦點時,onchange只會被觸發,所以onclick將首先被觸發。 –