【发布时间】:2026-02-10 16:55:02
【问题描述】:
我确定这应该是一个简单的问题,但我仍在学习,所以就这样吧:
我有一些代码可以在点击时运行一个函数,将点击的元素的 ID 分配给一个变量,但我不知道如何在不创建全局变量的情况下将“this.id”值传递给命名空间(我认为很糟糕)。
<script>
fsa = (function() {
function GetTemplateLoc() {
templateId = document.activeElement.id;
alert(templateId + templateId2);
}
return {
GetTemplateLoc: GetTemplateLoc,
}
})();
//call the functions
$(document).on('click', '.template', function () {
fsa.GetTemplateLoc();
});
</script>
和带有随机图片的 HTML:
<img id="template-1" class="template" src="http://fc02.deviantart.net/fs70/f/2010/028/c/b/cb21eda885b4cc6ee3f549a417770596.png"/>
<img id="template-2" class="template" src="http://fc02.deviantart.net/fs70/f/2010/028/c/b/cb21eda885b4cc6ee3f549a417770596.png"/>
【问题讨论】:
-
那么明显的
fsa.GetTemplateLoc(this.id);呢? -
该死的……当然。谢谢,正如你所知,我已经编程了几个星期了 :)
标签: javascript jquery namespaces this