jQuery每个都不起作用

时间:2013-09-05 10:41:03

标签: javascript jquery each

我正在尝试为选择器中的每个项目设置自定义宽度和高度,但它不起作用。我不知所措。一切看起来都对我不对。我错过了语法错误或其他什么? 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>

3 个答案:

答案 0 :(得分:2)

试试这个:

<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>

答案 1 :(得分:1)

尝试

<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>

添加了脚本标记,以防您在问题中发布的是完整标记

答案 2 :(得分:0)

有点优化的版本:



$( document ).ready(function() {
    $( "#posts" ).find( ".view" ).each(function(){
        console.log('Testing');
        $(this).css("height", 400);  
    });
});