JavaScript單獨的模塊中傳遞數據

首先咱們來看看這張圖,讓咱們來思考一下!css

下買我給出個人完整思路代碼html

html代碼:jquery

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
<input type="button" value="button1" id="button1" />
<input type="button" value="button2" id="button2" />
<script src="https://cdn.bootcss.com/jquery/1.8.1/jquery.min.js"></script>
<script src="test1.js"></script>
</body>
</html>

 

test1.js代碼spa

(function ($) {

    var userName = "用戶名稱2";

    function getEntity() {
        alert(userName);
    }

    $('#button1').click(function () {
        refFun(getEntity);
    })


})(jQuery);



(function ($) {

    var userName = "用戶名稱1"

    function getEntity() {
        alert(userName);
    }

    $('#button2').click(function () {
        refFun(getEntity);
    })


})(jQuery);

function refFun(fnName) {
    if (typeof fnName == "function") {
        fnName();
    }
}

 

 

 

若是這篇文章對您有幫助,您能夠打賞我code

 

技術交流QQ羣:15129679cdn

相關文章
相關標籤/搜索