最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery each not working - Stack Overflow

programmeradmin3浏览0评论

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.

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

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.

<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>
Share Improve this question edited Sep 5, 2013 at 19:33 user1966246 asked Sep 5, 2013 at 10:41 user1966246user1966246 151 gold badge1 silver badge3 bronze badges 8
  • Do you get any error message in the console? – Richard Dalton Commented Sep 5, 2013 at 10:42
  • looks fine to me jsfiddle/arunpjohny/53R3T/1 – Arun P Johny Commented Sep 5, 2013 at 10:43
  • Works fine for me: jsfiddle/qz8Lm – Darren Commented Sep 5, 2013 at 10:45
  • @ArunPJohny jsfiddle load jquery automatically...may be OP missing that part. – Dipesh Parmar Commented Sep 5, 2013 at 10:45
  • 1 Are you trying console.log in IE? This has some known issues. Failing that, I can't see any errors and the above fiddle looks alright to me. I suggest trying a few different browsers and perhaps different debugging software (firebug can be handy). EDIT: As mentioned above, if you're having CSS clashes - this can cause issues. Or, if you're not including the jQuery library, it wont work at all. – Myles Commented Sep 5, 2013 at 10:46
 |  Show 3 more ments

3 Answers 3

Reset to default 2

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>

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

A bit optimized version:



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

发布评论

评论列表(0)

  1. 暂无评论