在模块模式中访问 jQuery 对象

发布于 2024-10-12 04:49:24 字数 1473 浏览 5 评论 0原文

真正深入了解 javascript 并研究一些模式。我遇到的一种是模块模式。这似乎是一种思考功能块的好方法,所以我继续尝试用 jQuery 来实现它。但我遇到了障碍。考虑下面的代码

    <!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <title>index</title>        
        <script type="text/javascript" charset="utf-8" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <script type="text/javascript" charset="utf-8">
            $(document).ready(function(){

                var TestClass2 = (function(){
                    var someDiv;            
                    return {
                        thisTest: function ()
                        {
                               someDiv = document.createElement("div");
                                   $(someDiv).append("#index");
                                   $(someDiv).html("hello");
                                   $(someDiv).addClass("test_class");
                        }
                    }
                })();

                TestClass2.thisTest();          

            });
        </script>

    </head>

    <body id="index" onload="">

        <div id="name">
            this is content
        </div>

    </body>
</html>

上面的代码警告div的html内容,然后添加一个类。它们都使用 jQuery 方法。问题是 .html() 方法工作正常,但我无法添加该类。没有错误结果,并且不会添加该类。这里发生了什么?为什么类没有添加到 div 中?

Really getting in to javascript and looking around at some patterns. One I have come accross is the module pattern. Its seems like a nice way to think of chucks of functionality so I went ahead and tried to implement it with jQuery. I ran in to a snag though. Consider the following code

    <!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <title>index</title>        
        <script type="text/javascript" charset="utf-8" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <script type="text/javascript" charset="utf-8">
            $(document).ready(function(){

                var TestClass2 = (function(){
                    var someDiv;            
                    return {
                        thisTest: function ()
                        {
                               someDiv = document.createElement("div");
                                   $(someDiv).append("#index");
                                   $(someDiv).html("hello");
                                   $(someDiv).addClass("test_class");
                        }
                    }
                })();

                TestClass2.thisTest();          

            });
        </script>

    </head>

    <body id="index" onload="">

        <div id="name">
            this is content
        </div>

    </body>
</html>

The above code alerts the html content of the div and then adds a class. These both use jQuery methods. The problem is that the .html() method works fine however i can not add the class. No errors result and the class does not get added. What is happening here? Why is the class not getting added to the div?

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

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

发布评论

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

评论(3

北凤男飞 2024-10-19 04:49:24

啊,既然你已经更新了你的问题,我可以更好地回答你的问题了。考虑到您想要将新创建的元素移动到已经存在的#index 内,您应该将追加更改为appendTo。

$(document).ready(function() {

var TestClass2 = (function() {
    var someDiv = $("#name");
    return {
        thisTest: function() {
            someDiv = document.createElement("div");
            $(someDiv)
                .html("hello")
                .addClass("test_class")
                .appendTo("#index");
        }
    }
})();

TestClass2.thisTest();

});

希望这有帮助!

Ah, now that you've updated your question I can better answer your question. You should change the append to appendTo considering you're wanting to move the newly created element inside of the already present #index.

$(document).ready(function() {

var TestClass2 = (function() {
    var someDiv = $("#name");
    return {
        thisTest: function() {
            someDiv = document.createElement("div");
            $(someDiv)
                .html("hello")
                .addClass("test_class")
                .appendTo("#index");
        }
    }
})();

TestClass2.thisTest();

});

Hope this helps!

李白 2024-10-19 04:49:24

我复制并粘贴了您的代码,它对我有用。

确保您不是简单地查看源代码来查看该类是否已应用,因为这样做只是向您显示从服务器发送的 HTML - 通过 JavaScript 发生的任何 DOM 更新都不会反映。

要查看实时 DOM,请使用 Firebug 或 WebKit 的 Inspector(内置于 Safari 和 Chrome)等工具。

I copied and pasted your code and it works for me.

Make sure you're not simply viewing source to see if the class is applied because doing so simply shows you the HTML that was sent from the server - any DOM updates that occur through JavaScript will not be reflected.

To view the live DOM, use a tool like Firebug or WebKit's Inspector (comes built-in to Safari and Chrome).

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