jQuery无法访问文档的某些部分
我创建了一个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'
“.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容器上时,不会发生任何事情
问题:使用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>