jQuery无法访问文档的某些部分

jQuery无法访问文档的某些部分,jquery,Jquery,我创建了一个jQuery文档,但是它不能在一种类型的元素中获取任何元素,“.media\u container” 以下代码以这种方式运行: jQuery(document).ready(function($) { alert('alive'); $('h1').css('background', 'pink'); $('.media_container').hover(function(){ $(this).css('display', 'none'

我创建了一个jQuery文档,但是它不能在一种类型的元素中获取任何元素,
“.media\u container”

以下代码以这种方式运行:

jQuery(document).ready(function($) {

    alert('alive');

    $('h1').css('background', 'pink');

    $('.media_container').hover(function(){
        $(this).css('display', 'none');
    });
});
  • 'alive'
    已发出警报,因此我知道文件正在正确读取
  • 除了
    .media\u container
    内的标题外,所有标题都变为粉红色

  • 当我将鼠标悬停在任何
    .media\u容器上时,不会发生任何事情
  • 我在同一个php页面上使用不同的jQuery文件时遇到了同样的问题。 我试过了。mouseover和。mouseenter都没有反应


    问题:使用jQuery时,文档的某些部分无法通过$()访问,并且无法访问的部分只在一个元素类型中,.media\u container-知道为什么会这样吗?

    我已经为此挣扎了几天,当然,事后看来,原因和解决方案似乎很容易

    原因:jQuery在顺序和时间方面真的很挑剔

    • jQuery(document).ready()在将所有元素写入DOM时运行
    • 我们使用ajax将“.media\u”容器打印到页面上
    脚本运行时,页面如下所示:

    <html>
    <head>
    <script>
        window.onload(){
            loadContent(); //which uses ajax to inject php into the html
        }
    </script>
    </head>
    <body>
    
          //empty
    
          <script>
              jQuery(document).ready(function(){
                  $('.media_container').someFunction();
              });
          </script>
    </body>
    </html>
    
    解决方案:
    ,其中在
    头部运行的脚本文件中定义了attachFunctions(),问题就解决了
    新代码:

    <html>
        <head>
            <script>
                $(document).ready(function(){
                  loadContent(); //which uses ajax to inject php into the html
                });
                attachFunctions(){
                  $('.media_container').someFunction();
                }
            </script>
        </head>
        <body>
            <div class='media_container'></div>
            <div class='media_container'></div>
            <div class='media_container'></div>
            <div class='media_container' style='visibility:hidden'>
                <img src='spaceholder.jpg' onload='attachFunctions()'>
            </div>
        </body>
    </html>
    
    
    $(文档).ready(函数(){
    loadContent();//它使用ajax将php注入html
    });
    附件函数(){
    $('.media_container').someFunction();
    }
    
    能否提供HTML?
    媒体容器
    媒体容器
    ??显示你的htmlif.media_容器在iframe中jquery无法访问该容器。请查看它是否正常工作!
    <html>
        <head>
            <script>
                $(document).ready(function(){
                  loadContent(); //which uses ajax to inject php into the html
                });
                attachFunctions(){
                  $('.media_container').someFunction();
                }
            </script>
        </head>
        <body>
            <div class='media_container'></div>
            <div class='media_container'></div>
            <div class='media_container'></div>
            <div class='media_container' style='visibility:hidden'>
                <img src='spaceholder.jpg' onload='attachFunctions()'>
            </div>
        </body>
    </html>