如何自我识别脚本标签在 DOM 中的位置?

发布于 2024-09-05 19:32:23 字数 977 浏览 9 评论 0原文

可能有更好的方法来做到这一点,但目前我有一个封装良好的 JavaScript 对象,它可以有一些可配置的选项。我在页面上包含一段 HTML 代码(通过 Dreamweaver“片段”),在页面加载时,我的 JS 文件会通过 DOM 运行,并将这些代码块中的任何一个识别为特定功能,然后继续设置该功能。

这一切都很好,直到我希望在一页上添加多个对象,并让它们可配置。这里重要的一点是,您可以使用我当前的设置将任意数量的这些对象添加到页面上 - 因为它们在那时是通用的,并且没有“id”属性。

现在我希望配置这些对象,所以我想,“包含配置设置的外部文件怎么样,这些对象在找到配置对象时检查并应用该文件”。这也很好用,但是配置数据现在感觉有点“被删除”了。我想这最终会让我的其他同事感到烦恼,这只是另一件事要记住。

所以对于我的问题,我很高兴插入这些代码块,它们仍然会在页面加载时触发自实例化对象 - 但我想尝试的是插入一个包含配置设置的脚本块。我需要一种插入代码块的方法,知道它的父元素是配置的上下文。

示例代码:

<div class="snippet">
    <_contents of this 'snippet'_/>
    <script type="text/javascript">
        new Snippet().init({
            rootElement: REFERENCE_TO_THIS_SCRIPT_TAGS_PARENT_NODE,
            configOptionA: true,
            configOptionB: false
        });
    </script>
</div>

注意:

故意没有“id”属性,因为我希望允许将多个属性拖放到页面上。

欢迎其他解决方案,只要它们遵守我的一些限制!

There may be a better way of doing this, but currently I have an nicely encapsulated, JavaScript object which can have some configurable options. I include a chunk of HTML code on a page (via Dreamweaver 'snippets'), and on page load my JS file runs through the DOM and identifies any of those code chunks as a particular functionality, and goes ahead and sets that functionality up.

That's all fine up until I wish to add more than one object on a page, and have them be configurable. The important point here is that you can add as many of these objects onto a page as you like with my current setup - because they're generic at that point, and have no 'id' attribute.

Now I wish to configure these objects, so I thought, "How about an external file containing the config settings, which these objects check for and apply if a config object is found". This works fine too, but the config data feels a bit 'removed' now. I imagine this will be annoying for my other colleagues eventually, it's just another Thing To Remember.

So to my question, I'm happy to insert these code blocks which will still trigger self-instantiating objects on page load - but what I'd like to try is also inserting a script block which contains the config settings. I need a means of that inserted code block knowing that its parent element is the context for configuration.

Example code:

<div class="snippet">
    <_contents of this 'snippet'_/>
    <script type="text/javascript">
        new Snippet().init({
            rootElement: REFERENCE_TO_THIS_SCRIPT_TAGS_PARENT_NODE,
            configOptionA: true,
            configOptionB: false
        });
    </script>
</div>

Note: The <div class="snippet"> has no 'id' attribute on purpose, because I want to allow for more than one of these to be dropped onto a page.

Other solutions welcome, so long as they adhere to my few restrictions!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

镜花水月 2024-09-12 19:32:23

我的其他相关问题(现已回答)现在解决了这个问题,本质上我最终解决了with:

<div class="snippet">
    <elements... />
    <script type="text/javascript">
        var tmpVarName = 'configOb_'+Math.floor(Math.random()*1111111) ;
        document[tmpVarName] = {
            remainVisible:true,
            hoverBehaviour:false
        };        
    </script>
</div>

...然后在每个页面上加载的脚本中,该脚本扫描任何适当的元素以进行实例化和配置:

var snippets = yd.getElementsBy(function(el){
                    return yd.hasClass(el, "snippet");
                },null,document );
for( var i=0; i<snippets.length;i++ )
{
    var s = snippets[i] ;
        yd.generateId(s, '_snippet_'+i );
    var tag = yd.getElementBy(function(el){
                return true;
            },'script',s );
    var ob = new Snippet();
        ob.setId( s.id );
        ob.init( eval( tag.innerHTML ) );
}

对于上述代码的更完整的上下文;

  • yd = YAHOO.util.Dom
  • Snippet.init() 和 Snippet.setId() 是名为 Snippet() 的 Module 对象上的公开方法

现在我插入的内容“块”没有 id 属性,并且动态评估上下文配置对象- 我可以随意添加任意数量的变体。我唯一真正关心的是如果将一大堆这些 Snippet() 对象添加到我的页面中(不太可能)的性能。

My other related question (now answered) addresses this now, essentially I ended up with:

<div class="snippet">
    <elements... />
    <script type="text/javascript">
        var tmpVarName = 'configOb_'+Math.floor(Math.random()*1111111) ;
        document[tmpVarName] = {
            remainVisible:true,
            hoverBehaviour:false
        };        
    </script>
</div>

...and then in a script loaded on every page, which scans for any appropriate elements to instantiate and config:

var snippets = yd.getElementsBy(function(el){
                    return yd.hasClass(el, "snippet");
                },null,document );
for( var i=0; i<snippets.length;i++ )
{
    var s = snippets[i] ;
        yd.generateId(s, '_snippet_'+i );
    var tag = yd.getElementBy(function(el){
                return true;
            },'script',s );
    var ob = new Snippet();
        ob.setId( s.id );
        ob.init( eval( tag.innerHTML ) );
}

For a more complete context of the above code;

  • yd = YAHOO.util.Dom
  • Snippet.init() and Snippet.setId() are exposed methods on a Module object called Snippet()

Now that my inserted 'chunks' of content have no id attribute, and dynamically evaluated, contextual config objects - I am free to add as many variants as I like. My only real concern is performance if a whole bunch of these Snippet() objects are added to my page (not likely).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文