kik*_*ito 34 javascript ajax jquery
我的要求如下:
div
通过AJAX 加载一堆HTML .<script>...</script>
)$('document').ready( ... )
部分我有一个加载AJAX时调用的javascript函数.我试图通过这样做来"欺骗"执行:
function AjaxLoaded() {
$('document').trigger('ready');
}
Run Code Online (Sandbox Code Playgroud)我担心这不会削减它.
我已经看到Stack Overflow上的几个 响应通过更改AJAX上返回的代码来"逃避"这个问题(使其成为一个函数并在加载后调用它,或者只是删除它$(document).ready()
).我需要强调的是,我不能在这种情况下更改检索到的代码.
Joh*_*ker 21
在一些研究中,我创造了一种让它发挥作用的方法.
这是我的测试,显示它正常工作:http://www.antiyes.com/test/test2.php
这是相关代码:
<script>
// easy copy of an array
Array.prototype.copy = function() {
return [].concat(this);
};
// this function is added to jQuery, it allows access to the readylist
// it works for jQuery 1.3.2, it might break on future versions
$.getReadyList = function() {
if(this.readyList != null)
this.myreadylist = this.readyList.copy();
return this.myreadylist;
};
$(document).ready(function() {
alert("blah");
});
</script>
<script>
// this should be added last so it gets all the ready event
$(document).ready(function() {
readylist = $.getReadyList();
});
</script>
Run Code Online (Sandbox Code Playgroud)
然后在我的身体:
<input type="button" onclick="$(readylist).each(function(){this();});" value="trigger ready" />
Run Code Online (Sandbox Code Playgroud)
基本上我所做的是为jQuery添加一个函数,在它被清除之前复制readyList,然后它就可供你使用了.
它看起来像下面的代码不起作用:
function AjaxLoaded() {
$(document).trigger('ready');
}
Run Code Online (Sandbox Code Playgroud)
丢弃报价document
.
rak*_*oof 12
由于jQuery readyList在版本1.4(此处讨论)中未公开,因此上面的好解决方案被破坏了.
解决这个问题的方法是通过覆盖原始的jQuery-ready方法来创建自己的readyList.这需要在加载使用原始ready方法的其他脚本之前完成.否则只是与John/Kikito相同的代码:
// Overrides jQuery-ready and makes it triggerable with $.triggerReady
// This script needs to be included before other scripts using the jQuery-ready.
// Tested with jQuery 1.7
(function(){
var readyList = [];
// Store a reference to the original ready method.
var originalReadyMethod = jQuery.fn.ready;
// Override jQuery.fn.ready
jQuery.fn.ready = function(){
if(arguments.length && arguments.length > 0 && typeof arguments[0] === 'function') {
readyList.push(arguments[0]);
}
// Execute the original method.
originalReadyMethod.apply( this, arguments );
};
// Used to trigger all ready events
$.triggerReady = function() {
$(readyList).each(function(){this();});
};
})();
Run Code Online (Sandbox Code Playgroud)
我不确定是否建议覆盖ready方法.随意建议我.我自己还没有发现任何副作用.
万一有人需要它,我稍微改进了John的解决方案,因此它可以直接用作包含的javascript文件.
// jquery_trigger_ready.js
// this function is added to jQuery, it allows access to the readylist
// it works for jQuery 1.3.2, it might break on future versions
$.getReadyList = function() {
if(this.readyList != null) { this.myreadylist = [].concat(this.readyList); }
return this.myreadylist;
};
$(document).ready(function() {
readylist = $.getReadyList();
});
$.triggerReady = function() {
$(readylist).each(function(){this();});
}
Run Code Online (Sandbox Code Playgroud)
包含jquery后包含此文件允许通过调用触发准备$.triggerReady()
.例:
<html>
<head>
<title>trigger ready event</title>
<script src="test2_files/jquery-1.js" type="text/javascript"></script>
<script src="jquery_trigger_ready.js" type="text/javascript"></script>
</head>
<body>
<input onclick="$.triggerReady();" value="trigger ready" type="button">
<script type="text/javascript">
$(document).ready(function(){
alert("blah");
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
顺便说一句,我想成功$(document).triggerReady()
.如果有人愿意就此提出一些建议,我们将不胜感激.
小智 6
我们遇到了同样的问题并以另一种方式解决了.
代替
$(document).ready(function () {
$('.specialClass').click(....
Run Code Online (Sandbox Code Playgroud)
我们用了 :
$(document).bind('ready', function(event) {
$('.specialClass', event.target).click(..
Run Code Online (Sandbox Code Playgroud)
jQuery将像往常一样在文档上触发"就绪"事件.当我们通过ajax加载新div的内容时,我们可以写:
loadedDiv.trigger('ready')
Run Code Online (Sandbox Code Playgroud)
并且只在div上执行所有初始化,获得预期的内容.
归档时间: |
|
查看次数: |
25500 次 |
最近记录: |