0
<html>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script> 
        $("div.block").each(index){
            this.text('Fill this writing of divs with the classname "block"')} 
</script>

<body>
        <div>Not here</div> 
        <div class='block'>replace me -- not working, why?</div> 
        <div>Not here</div> 
        <div class='block'>replace me -- not working, why?</div>
</body>

</html>
4

1 に答える 1

4

You don't need to use each - you can just call the text method on your entire selection:

$("div.block").text('Fill this writing of divs with the classname "block"');

Incidentally, your problem above was that you were using each incorrectly

$("div.block").each(function(index, el){
    $(el).text('Fill this writing of divs with the classname "block"');
}); 
于 2012-11-22T23:32:22.037 に答える