在模块模式中访问 jQuery 对象
真正深入了解 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
啊,既然你已经更新了你的问题,我可以更好地回答你的问题了。考虑到您想要将新创建的元素移动到已经存在的#index 内,您应该将追加更改为appendTo。
希望这有帮助!
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.
Hope this helps!
我复制并粘贴了您的代码,它对我有用。
确保您不是简单地查看源代码来查看该类是否已应用,因为这样做只是向您显示从服务器发送的 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).
你的代码效果很好!
http://jsfiddle.net/lmcculley/p3fDX/
Your code works great!
http://jsfiddle.net/lmcculley/p3fDX/