jquery函式$.proxy簡單示例

php之路發表於2013-12-23

來自於《jquery 權威指南》

------------------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery</title>
<style type="text/css">
body,div,ul,li,p{margin: 0;padding: 0; font-size: 13px;}
ul{list-style-type: none;}
a{text-decoration: none;}
div{margin: 5px;padding: 10px;border: solid 1px #666;background-color: #eee;width: 260px;}
input{margin: 5px;}
.btn{border: solid 1px #666;padding: 2px; width: 50px;}

</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jqueryui/ui/jquery-ui.js"></script>
<script type="text/javascript">
    $(function(){
        var objMyInfo = {
            name: "王美麗",
            sex: "",
            ShowEvent: function(){
                $(".divShow").html("姓名:" + this.name + "<br />性別:" + this.sex);
            }
        };
        $("#btn1").bind("click",$.proxy(objMyInfo.ShowEvent,objMyInfo));
    });

</script>
</head>
<body>
<input id="btn1" type="button" value="顯示" class="btn" />
<div class="divShow"></div>
</body>
</html>

執行效果:

-----------------------------------------------------------------

#btn1的click事件,要呼叫其它作用域的事件函式,需要用到$.proxy工具函式。

相關文章