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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:41:03  来源:igfitidea点击:

jQuery each not working

javascriptjqueryeach

提问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);  
    });
});