本文實例講述了javascript實現相同事件名稱,不同命名空間的調用方法。分享給大家供大家參考。具體實現方法如下:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
$("div").bind("click",function(){
$("body").append("<p>click事件</p>");
});
$("div").bind("click.plugin", function(){
$("body").append("<p>click.plugin事件</p>");
});
$("button").click(function() {
$("div").trigger("click!"); // 注意click後面的感歎號
// click! 後面有歎號,是調用沒有任何命名空間的click事件
// click 後面沒有歎號,是調用所有click事件(不管是那個命名空間的)
// click.plugin 是調用某個特定命名空間的click事件(本例中是plugin)
});
})
</script>
</head>
<body>
<div style="width:100px;height:50px;background:#888;color:white;">test.</div>
<button >根據命名空間,觸發事件</button>
</body>
</html>
希望本文所述對大家的javascript程序設計有所幫助。