我目前正在使用无限滚动加载更多内容,该内容具有<script>
需要运行的标签。所以我将以下代码用作ajax-callback
:
JS加载ajax回调:
function ajaxLoadedCallback() {
// contentElem is the HTML element you've loaded via ajax
scriptx = document.getElementsByTagName("script");
// Load scripts into new array because
// scriptx seems to change as scripts execute, at least in some browsers
scripts = new Array();
for (var idx=0; idx<scriptx.length; idx++) {
if (jQuery(scriptx[idx]).is("#inline-comments")) {
scripts[idx] = scriptx[idx].text;
}
}
console.log (scripts[idx]);
// execute each script in turn
for(idx=0; idx<scripts.length; ++idx) {
if (scripts[idx].length!=0) {
try {
// create a function for the script & execute it
f = new Function(scripts[idx]);
f();
} catch(se) {
} // end try-catch
} // end if
} // end for
}
每个内容帖子都有我要加载的脚本标签:
<script id="inline-comments" >
console.log ('<?php echo $post->ID; ?>' + 'has loaded...');
var tid_<?php echo $post->ID; ?> = setInterval( function () {
if ( document.readyState !== 'complete' ) return;
clearInterval( tid_<?php echo $post->ID; ?> );
inline_comments_ajax_load(<?php echo $post->ID; ?>)
}, 100 );
</script>
获取未捕获的TypeError:无法在此行上读取未定义错误的属性“ length”: if (scripts[idx].length!=0) {
Ajax回调在正确的时间加载,但是由于我认为scripts[idx]
是空的而中断了。但为什么?当我使用console.log()
它时,显示时scriptx[idx]
具有ID和ID名称。
更新谢谢PSL!console.log
表明它正在查找脚本,但是scripts.length返回零
function ajaxLoadedCallback() {
scriptx = document.getElementsByTagName("script");
scripts = new Array();
for (var idx=0; idx<scriptx.length; idx++) {
if (jQuery(scriptx[idx]).is(".inline-comments-script")) {
console.log (".inline-comments-scriptfound!");
console.log ("####### .text #######");
console.log (scriptx[idx].text);
console.log ("####### .innerHTML ######");
console.log (scriptx[idx].innerHTML);
scripts.push = scriptx[idx].innerHTML;
}
}
console.log ("####### END ######");
console.log ("Found "+scripts.length+ " script(s)");
// execute each script in turn
for(idx=0; idx<scripts.length; ++idx) {
var content = scripts[idx];
if (content.length) {
try {
// create a function for the script & execute it
f = new Function(content);
f();
} catch(se) {
} // end try-catch
} // end if
} // end for
}
现在工作!请参阅下面的PSL答案-如果您拥有wordpress,也可以实时查看它:https : //github.com/MattMcFarland/inline-ajax-comments-很棒!
您这里有几个问题。
for (var idx=0; idx<scriptx.length; idx++) {
if (jQuery(scriptx[idx]).is("#inline-comments")) {
scripts[idx] = scriptx[idx].text;
}
}
即使没有匹配项,您也要递增循环迭代变量,并且您不希望将任何项目推入数组。因此,如果第一个脚本与您的条件不匹配,scripts[0]
则将无法定义条件,因此当您length
在条件中访问undefined属性时会收到错误消息if (scripts[idx].length!=0) {
另一个问题是scriptx[idx].text
这里的文本是一个函数,因此您需要元素的真实文本而不是函数引用,因此您应该编写jQuery(scriptx[idx]).text()
或scriptx[idx].innerHTML
。由于您使用的是id并且id不应该重复,因此可以简单地写成:
var txt = jQuery('#inline-comments').text();
if (txt.length) {
try {
// create a function for the script & execute it
f = new Function(scripts[idx]);
f();
} catch(se) {
} // end try-catch
} // end if
如果您打算inline-comments
从ID转到课程,并且希望访问其中的多个课程,则应该可以使用。
scripts = [];
for (var idx=0; idx<scriptx.length; idx++) {
if (jQuery(scriptx[idx]).is(".inline-comments")) {
scripts.push(scriptx[idx].innerHTML);
}
}
// execute each script in turn
for(idx=0; idx<scripts.length; ++idx) {
var content = scripts[idx];
if (content.length) {
try {
// create a function for the script & execute it
f = new Function(content);
f();
} catch(se) {
} // end try-catch
} // end if
} // end for
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句