遍历每个html标记

时间:2012-06-27 12:34:24

标签: javascript jquery

我想将每个圆圈放在一个div中,这样我就可以使用jquery调整它的大小。我的代码如下:

<div id="DG_circles">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="200" height="75">
<desc>Created with Raphaël</desc>
<defs>
<circle cx="39" cy="39.5" r="16" fill="#d57b19" stroke="none" style="stroke-width: 0;" stroke-width="0">
<image x="28.8" y="29.3" width="22.4" height="22.4" preserveAspectRatio="none" href="load?name=icon-hotel-chart">
<text x="39" y="17.5" text-anchor="middle" style="text-anchor: middle; font: bold 9px Arial,Verdana,Courier; stroke-width: 0;" font="10px "Arial"" stroke="none" fill="#333333" font-size="9px" font-family="Arial, Verdana, Courier" stroke-width="0" font-weight="bold">
<circle cx="91" cy="39.5" r="16" fill="#b1102b" stroke="none" style="stroke-width: 0;" stroke-width="0">
<image x="80.8" y="29.3" width="22.4" height="22.4" preserveAspectRatio="none" href="load?name=icon-card-chart">
<text x="91" y="17.5" text-anchor="middle" style="text-anchor: middle; font: bold 9px Arial,Verdana,Courier; stroke-width: 0;" font="10px "Arial"" stroke="none" fill="#333333" font-size="9px" font-family="Arial, Verdana, Courier" stroke-width="0" font-weight="bold">
<circle cx="143" cy="39.5" r="16" fill="#85bb3c" stroke="none" style="stroke-width: 0;" stroke-width="0">
<image x="132.8" y="29.3" width="22.4" height="22.4" preserveAspectRatio="none" href="load?name=icon-airplane-chart">
<text x="143" y="17.5" text-anchor="middle" style="text-anchor: middle; font: bold 9px Arial,Verdana,Courier; stroke-width: 0;" font="10px "Arial"" stroke="none" fill="#333333" font-size="9px" font-family="Arial, Verdana, Courier" stroke-width="0" font-weight="bold">
</svg>
</div>​


var $circle = $('#DG_circles').find("circle");

$circle.each(function(index, value){
    if(index == 0){
        $this.attr("r", "24");
    } else if(index == 1){
        $this.attr("r", "20");
    } else if(index == 2){
        $this.attr("r", "18");
    }
});

但它不会改变圆圈的“r”属性。 请帮忙。 谢谢。

3 个答案:

答案 0 :(得分:3)

为什么你甚至需要在这里使用each

var $circle = $('#DG_circles').find("circle");
$circle.eq(0).attr("r", "24");
$circle.eq(1).attr("r", "20");
$circle.eq(2).attr("r", "18");

答案 1 :(得分:1)

this是一个dom元素,而不是jQuery。你必须将它转换为jQuery元素才能使用jQuery函数。

$(this).attr('r', 24);

答案 2 :(得分:1)

...试

var circle = $('#DG_circles').find("circle");

circle.each(function(index, value){
    if(index == 0){
        $(this).attr("r", "24");
    } else if(index == 1){
        $(this).attr("r", "20");
    } else if(index == 2){
        $(this).attr("r", "18");
    }
});