當使用處理器的這種風格,從處理程序代碼返回false
:
<a4j:commandButton value="TestButton" onclick="alert('button clicked'); return false;"/>
作爲theatrus said,不過,如果你的目標是禁用按鈕,你最好還是實際上禁用按鈕,而不是防止點擊它。但就您的問題而言,「如何禁用左鍵點擊」,這就是答案。
更徹底,有兩種不同的方式附加事件處理程序:
DOM0(你使用的樣式):在處理程序代碼返回false
停止活動(防止起泡起來,其他的元素)和防止默認動作:
<someElement onclick="return false;">
DOM2(用JavaScript代碼):DOM2處理程序經由addEventListener
附接(或attachEvent
,上IE)。下面是一個例子,假設一個元件與id
「foo」 的:
// Attaching:
var elm = document.getElementById("foo");
if (elm.attachEvent) {
elm.attachEvent("onclick", handler);
}
else if (elm.addEventListener) {
elm.addEventListener("click", handler, false);
}
// Handler:
function handler(event) {
// DOM standard is that `event` is an argument to the
// handler; IE uses a global event object instead.
// This line handles that.
event = event || window.event;
// Cancelling bubbling:
if (event.stopPropagation) {
// DOM standard
event.stopPropagation();
}
else {
// Older mechanism
event.cancelBubble = true;
}
// Preventing default action:
if (event.preventDefault) {
// DOM standard
event.preventDefault();
}
else {
// Older mechanism
event.returnValue = false;
}
}
一些基準物質:
不是' onclick =「alert(」button cliked「)」'語法錯誤?另外,出於好奇,你的用例是什麼? – deceze 2010-06-28 05:47:55