代码之家  ›  专栏  ›  技术社区  ›  Johann

jQuery未在jsFiddle中执行

  •  -1
  • Johann  · 技术社区  · 7 年前

    下面是一些简单的jquery代码的链接,我正试图在jfiddle中运行这些代码,但它会生成一个异常,即文本不是函数:

    <div class="animals">
      <div class="cats">
         <div class="lions">
         meow
         </div>
      </div>
    </div>
    

    Javascript:

    jQuery.find(".animals .lions").text("purr");
    jQuery.find(".animals").text("kitty");
    

    https://jsfiddle.net/Deepview/khbwqrte/5/

    2 回复  |  直到 7 年前
        1
  •  2
  •   Roy Bogado    7 年前

    您必须将jquery lib添加到jfiddle上的资源中: https://jsfiddle.net/1jcyb0qu/1/
    使用

    $(".animals .lions").text("purr");
    $(".animals").text("kitty");
    

    来自@猛禽: https://stackoverflow.com/a/15631604/7349445

    当调用.noconflict()时,类似于$(“”)的选择器不再工作。 以确保与其他框架(如原型)的兼容性。在 此时使用jquery(“”)。

    参考文献: http://api.jquery.com/jQuery.noConflict/

    为了更好地说明这个想法,下面是从 参考链接:

    <script type="text/javascript">
      $.noConflict();
      jQuery(document).ready(function($) {
        // Code that uses jQuery's $ can follow here.
      });
      // Code that uses other library's $ can follow here.
    </script>
    
        2
  •  2
  •   Sushil    7 年前

    $ 定义为 jQuery 在jquery库中,因此您可以使用 $ JQuery . 你不能用 jQuery.find() 你能做的就是 jQuery(document).find()

    jQuery(".animals .lions").text("purr");
    jQuery(".animals").text("kitty");
    

    $(".animals .lions").text("purr");
    $(".animals").text("kitty");
    

    jQuery(document).find(".animals .lions").text("purr");
    jQuery(document).find(".animals").text("kitty");