在对象文字中调用 jQuery.load
我正在尝试学习一些更高级的 java 脚本模式,同时使用 jQuery 来缓解 jQuery 的一些 ajax 困难。但是,我一直遇到没有错误的问题。
我希望能够调用 ajaxCalls.setup({}) 并让它用传递给 setup 的对象替换默认值。我有那部分工作。我似乎遇到的问题是 ajax 调用。当 callIt 函数运行时, $(this.defaults.element).load(....) 不会运行,或者即使运行也会失败。我很困惑。我真的只是想学习新东西。我将不胜感激任何帮助
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script>
var ajaxCalls = (function($){
var ajaxer = {
defaults:{
url:"test.php",
element:"#ajaxerizer"
},
setup:function(setup){
var defaulLengther = this.defaults
/*var l = 0;*//*bug*/
for (var key in defaulLengther)
{
if(setup.hasOwnProperty(key))
{
this.defaults[key] = setup[key];
/*l++;*//*bug*/
}
}
/*debugger*/
/*for(var i=0; i < l; i++)
{
alert(this.defaults.power);
} */
this.callIt();
},
callIt:function(){
$(this.defaults.element).load(this.defaults.url, function(){alert("success");});
},
}
return ajaxer
})(jQuery);
ajaxCalls.setup({})
</script>
</head>
<body>
<div id="ajaxerizer"></div>
</body>
</html>
谢谢你的帮助 test.php 只是有这个
<p>IT WORKED</p>
注释:在这个项目中我打算只使用 jQuery 进行 DOM 选择和 aJax 调用
I am trying to learn some of the more advanced java scripting patterns while using jQuery to ease some of the ajax difficulty with jQuery. However, I keep running in to problem that throws no errors.
I want to be able to call the ajaxCalls.setup({}) and have it replace the defaults with the object passed to setup. I have that part working. What I seem to have problems with is the ajax call. When the callIt function runs the $(this.defaults.element).load(....) does not run or if it does it fails. I am pretty stumped. I am really just trying to learn something new. I would appreciate any help
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
<script>
var ajaxCalls = (function($){
var ajaxer = {
defaults:{
url:"test.php",
element:"#ajaxerizer"
},
setup:function(setup){
var defaulLengther = this.defaults
/*var l = 0;*//*bug*/
for (var key in defaulLengther)
{
if(setup.hasOwnProperty(key))
{
this.defaults[key] = setup[key];
/*l++;*//*bug*/
}
}
/*debugger*/
/*for(var i=0; i < l; i++)
{
alert(this.defaults.power);
} */
this.callIt();
},
callIt:function(){
$(this.defaults.element).load(this.defaults.url, function(){alert("success");});
},
}
return ajaxer
})(jQuery);
ajaxCalls.setup({})
</script>
</head>
<body>
<div id="ajaxerizer"></div>
</body>
</html>
Thank you for your help test.php just has this in it
<p>IT WORKED</p>
a note: in this project I intended to only use jQuery for DOM selection and aJax calls
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 document.ready 将 ajaxCalls.setup 设置为在页面加载后运行
例如。
否则你会尝试在加载 html 之前替换你的 div 。
You need to set your ajaxCalls.setup to run after the page is loaded using document.ready
eg.
Otherwise you're trying to replace your div before the html is loaded.