如何使用jQuery更改DIV中的HTML?

时间:2013-05-15 18:58:34

标签: javascript jquery html css

示例:

    for (i=1; i <= 3; i++) {
        alert(i + "," + wanted);
        $("#hint-" + $i).html("Verified as " + $i + ".<br />");
    }

更改此内容:

<div id="verify-1"> </div>
<input type="radio" name="b_answer_1" id="radio-1" value="Lamb">Lamb</input><br />
<div id="verify-2"> </div>
<input type="radio" name="b_answer_2" id="radio-2" value="Llama">Llama</input><br />
<div id="verify-3"> </div>
<input type="radio" name="b_answer_3" id="radio-3" value="Goat">Goat</input><br />

对此:

                    <div id="verify-1">Verified as 1<br /></div>
<input type="radio" name="b_answer_1" id="radio-1" value="Lamb">Lamb</input><br />
                    <div id="verify-2">Verified as 2<br /></div>
<input type="radio" name="b_answer_2" id="radio-2" value="Llama">Llama</input><br />
                    <div id="verify-3">Verified as 3<br /></div>
<input type="radio" name="b_answer_3" id="radio-3" value="Goat">Goat</input><br />

2 个答案:

答案 0 :(得分:3)

$

之前移除i
for (i=1; i <= 3; i++) {
        alert(i + "," + wanted);
        $("#verify-" + i).html("Verified as " + i + ".<br />");
}

或者你可以这样做 -

$("div[id^='verify-']").each(function(){
    var num = this.id.match(/(\d+)$/)[1];
    $(this).html("Verified as " + num + ".<br />");
});

答案 1 :(得分:2)

您的ID是verify-1 , verify-2 ...等

for (i=1; i <= 3; i++) 
    $("#verify-" + i).html("Verified as " + i + ".<br />");