简体   繁体   English

html,js-如何限制元素“范围”-命名空间

[英]html, js - how to limit elements “scope” - namespaces

During last perioud I've seen more and more often the following situation. 在上一讲期间,我越来越经常看到以下情况。

Developer A creates a feature. 开发人员A创建功能。 Let's say is an autocomplete input. 假设是自动完成输入。 Let's assume for simplicity that no framework is used, html and js are on the same file. 为了简单起见 ,我们假设未使用任何框架,并且html和js位于同一文件中。 The file would look like this (let's also asume jquery used - is less to type): 该文件看起来像这样(我们也假设使用了jquery-键入较少):

<!-- autocomplete something.html file -->
.........................................
<input id="autocomplete" type="text" />

<script type="text/javascript">
    $('#autocomplete').change(function() {
        // Do lots of things here -> ajax request, parsing, etc.
    });
</script>
.........................................

Now developer B sees the feature and says "Oh, nice feature, I can use that in my section, I'll put one at the top, and one at the bottom - because page is long". 现在,开发人员B看到了该功能,并说:“哦,很好的功能,我可以在本节中使用它,我将在顶部放置一个,在底部放置一个-因为页面很长”。

And he does an ajax call to get that html file (this is important, I'm talking about features loaded like this, not features rewritten for the other section) and includes it where he needs it. 并且他进行了ajax调用以获取该html文件 (这很重要,我正在谈论的是这样加载的功能,而不是为另一部分重写的功能),并在需要时将其包括在内。

Now... problem. 现在...问题。 The first autocomple works, the second doesn't (because is select by id). 第一个自动补全有效,第二个无效(因为通过id选择)。

A workaround would be to modify, and use a class. 解决方法是修改并使用一个类。 And everything is ok, unless someone else (or himself, or whatever) uses same class for a tottaly different thing. 一切都会好起来的,除非别人(或他自己,或其他)使用相同的类来处理完全不同的事情。

This could all be avoided if you could the the script to use as a "scope" (I know it is not the correct phrasing, couldn't find any better) the file where it was declared on. 如果可以将该脚本用作声明其的文件(我知道这不是正确的措词,找不到更好的方法),则可以全部避免。

Note: This is a theoretical question. 注意:这是一个理论问题。 For each particular case a solution could be found, but defining some kind of namespaces for this scenarios would solve the whole class of problems. 对于每种特定情况,都可以找到解决方案,但是为此方案定义某种名称空间将解决整个问题。

How could that be achieved? 如何实现?

As long as you're accessing the DOM the only way I see would be to use the "old" inline event: 只要您访问DOM,我看到的唯一方法就是使用“旧”内联事件:

<!-- autocomplete something.html file -->
.........................................
<input onchange="autocomplete_change();" type="text" />

<script type="text/javascript">
    function autocomplete_change() {
        // Do lots of things here -> ajax request, parsing, etc.
    };
</script>
...........

Now the function name cannot be used by Developer B. But when accessing DOM some kind of restriction will always be there. 现在,开发人员B无法使用该函数名称。但是,在访问DOM时,总是会存在某种限制。

However when creating the input by Script you can bind the handler directly to the Element: 但是,通过脚本创建输入时,可以将处理程序直接绑定到Element:

<script>
    var input = $('<input type="text"/>');

    input.change(function() {
        // Do lots of things here -> ajax request, parsing, etc.
    });
    document.body.appendChild(input[0]);
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM