页面上的iframe中的jQuery不起作用

时间:2017-03-21 08:29:21

标签: javascript jquery html facebook iframe

我在使用iframe的页面中制作APPENDED jQuery时遇到了一些麻烦。我要么

  

未捕捉错误:

  

未捕获的ReferenceError:$未定义

以下部分负责将脚本附加到iframe上显示的页面:

var simpleScriptTest = '<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">'+
'</script>'+
'<script>'+
  '$("a").hover(function(){'+   
  ' $(this).css("background-color", "rgb(255, 255, 0)");'+
  '});'+
'</script>';

$('iframe').on("load",function(){
  loadedIframe = true;
  $("iframe").contents().find('script').remove();
  $("iframe").contents().find('noscript').remove();
  $("iframe").contents().find("head").append(simpleScriptTest);
  console.log('load the iframe');
});

任何人都可以解释为什么会发生这种情况以及如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

在加载之前,您似乎调用了jquery $。 这一个:

$('iframe').on("load",function(){
  loadedIframe = true;
  $("iframe").contents().find('script').remove();
  $("iframe").contents().find('noscript').remove();
  $("iframe").contents().find("head").append(simpleScriptTest);
  console.log('load the iframe');
});

在实际加载jquery(append(simpleScriptTest))到页面之前,会调用jquery的$(&#39; iframe&#39;)。这就是为什么你的页面不理解$?或者你在其他地方已经有jquery链接了吗?