javascript jQuery 每个都不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18634147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery each not working
提问by user1966246
I am trying to set a custom width and height for each item in the selector and it won't work. I'm at a loss. Everything looks correct to me. Am I missing a syntax error or something? The console.log doesn't even work.
我正在尝试为选择器中的每个项目设置自定义宽度和高度,但它不起作用。我不知所措。在我看来,一切都是正确的。我错过了语法错误还是什么?console.log 甚至不起作用。
<div id="posts">
<article>
<div class="view">
...
</div>
</article>
<article>
<div class="view">
...
</div>
</article>
...
</div>
<script>
$( document ).ready(function() {
$( "#posts .view" ).each(function(){
console.log('Testing');
$(this).css("height", "400px");
});
});
</script>
回答by Myles
Try this:
试试这个:
<div id="posts">
<article>
<div class="view">
...
</div>
</article>
<article>
<div class="view">
...
</div>
</article>
...
<script>
$( document ).ready(function() {
$( "#posts .view" ).each(function(){
console.log('Testing');
$(this).css("height", "400px");
});
});
</script>
回答by DGS
Try
尝试
<div id="posts">
<article>
<div class="view">
...
</div>
</article>
<article>
<div class="view">
...
</div>
</article>
...
</div>
<script>
$( document ).ready(function() {
$( "#posts .view" ).each(function(){
console.log('Testing');
$(this).css("height", "400px");
});
});
</script>
Added script tags in case what you posted in your question was the full markup
添加脚本标签,以防您在问题中发布的内容是完整标记
回答by lshettyl
A bit optimized version:
稍微优化的版本:
$( document ).ready(function() {
$( "#posts" ).find( ".view" ).each(function(){
console.log('Testing');
$(this).css("height", 400);
});
});