Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 计算类的实例-使用Firebug将jQuery注入第三方网站?_Javascript_Jquery_Html_Firebug - Fatal编程技术网

Javascript 计算类的实例-使用Firebug将jQuery注入第三方网站?

Javascript 计算类的实例-使用Firebug将jQuery注入第三方网站?,javascript,jquery,html,firebug,Javascript,Jquery,Html,Firebug,我正在访问带有评论部分的第三方网页 相关网站: “注释”部分仅显示少量注释,并显示“显示更多”按钮。我正在编写一个脚本,将继续自动按下“显示更多”按钮,直到所有的评论都显示出来。一旦“显示更多”按钮不再显示,我想触发一个新功能 numShowMore = $('.vf-load-more').length; if (numShowMore == 0) { clearInterval(interval1); nextFunction(); //This function has n

我正在访问带有评论部分的第三方网页

相关网站:

“注释”部分仅显示少量注释,并显示“显示更多”按钮。我正在编写一个脚本,将继续自动按下“显示更多”按钮,直到所有的评论都显示出来。一旦“显示更多”按钮不再显示,我想触发一个新功能

numShowMore = $('.vf-load-more').length;
if (numShowMore == 0) {
    clearInterval(interval1);
    nextFunction(); //This function has not been written yet
}
我试图通过Firefox(30.0)中Firebug(2.0.1)中的命令行将我自己的JavaScript“注入”到第三方页面来实现这一点

使用Firebug,我发现“显示更多”按钮的类是
vf load More
。我创建了一个
setInterval()
函数调用,并且能够成功地反复“单击”按钮

现在,我正在尝试实现“评论结束”识别。我通过计算页面上
vf load more
的实例来实现这一点。如果没有
vf load more
的实例,则启动下一个功能

numShowMore = $('.vf-load-more').length;
if (numShowMore == 0) {
    clearInterval(interval1);
    nextFunction(); //This function has not been written yet
}
问题:
$(“.vf加载更多”).length正在返回未定义的
。为什么?按钮位于页面上的

完整的代码:

//This injects a jQuery reference into the header
var script = document.createElement('script');
script.src = '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js';
document.body.appendChild(script);

//If jQuery is loaded execute the rest of the code
if (!window.jQuery)
{
  alert('jQuery is GO');

  var numShowMore;

  function clickButton()
  {
    numShowMore = $('.vf-load-more').length;
    alert('numShowMore = ' + numShowMore); //This alert returns "undefined"??
    if (numShowMore == 0) {
      clearInterval(interval1);
      nextFunction(); //This function has not been written yet
    }
    $('.vf-load-more').click();
  }

  var interval1 = setInterval(clickButton, 3000);
}
现在它变得奇怪了。如果我让间隔继续运行并“单击”“显示更多”按钮,直到显示所有注释,它最终将停止显示“显示更多”按钮。然后,我通过运行
clearInterval(interval1)强制间隔停止在命令编辑器中,然后当我运行以下代码时,它会发出警报
numShowMore=1
,但按钮不再存在

numShowMore = $('.vf-load-more').length;
alert('numShowMore = ' + numShowMore);

为什么不是0?

我试着用
jQuery
替换jQuery
$
,结果成功了。我不知道为什么,但我认为jQuery可能与页面上加载的另一个库发生冲突

$('.vf-load-more').length;
被替换为

jQuery('.vf-load-more').length;
jQuery('.vf-load-more').click();

被替换为

jQuery('.vf-load-more').length;
jQuery('.vf-load-more').click();
我还注意到您的代码中有一些错误,在检查jQuery是否加载的if条件中,该条件被颠倒了,应该是

if (window.jQuery){
//do stuff
}
但是,由于您正在加载一个可能需要时间加载的外部脚本,如果未加载库,则条件可能为false,因此最好使用
setInterval
setTimeout
检查jQuery是否加载,以及是否加载,然后执行代码

大概是这样的:

//If jQuery is loaded execute the rest of the code
var jqueryCheckTimer = setInterval(function(){
if(window.jQuery){
        clearInterval(jqueryCheckTimer);
         var interval1 = setInterval(clickButton, 3000);
    }
},100);

我接受你的答案。如果我将“$”的所有实例都更改为“jQuery”,它仍然不起作用,因此我发现我必须选择在何处进行更改才能使其起作用(例如,我仍然必须将“$”与.click()一起使用)调用或按钮未单击。我选择您的解决方案的主要原因是,我还认为这是一个相互冲突的框架问题,您已将我引向jQuery.noConflict()的方向,我相信这将大大有助于解决我遇到的所有问题。谢谢!