如何用jQuery替换某个类中的Div -content?

时间:2012-11-22 23:31:36

标签: javascript jquery dom

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

1 个答案:

答案 0 :(得分:3)

您不需要使用每个 - 您可以在整个选择中调用文本方法:

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

顺便提一下,上面的问题是你错误地使用了each

$("div.block").each(function(index, el){
    $(el).text('Fill this writing of divs with the classname "block"');
});