2012-01-13 52 views
-1

有人可以告訴我如何將「top」屬性添加到第二個「canvas」標記,該標記在div中沒有​​id,並且可以使用jquery或其他任何工具在IE中工作。以下是HTML代碼片段。將屬性添加到div中的第二個孩子

<div id="jqChart" class="ui-jqchart" style="height: 300px;"> 
<canvas width="841" height="300" style="position: relative;"></canvas> 
<canvas width="794" height="222" style="position: relative; left: 37px; top: 42px;">  </canvas> 
<div class="ui-jqchart-tooltip" style="position: absolute; display: none;"></div> 
</div> 

回答

0

您可以使用此:

$("#jqChart > canvas:nth-child(2)").attr("attribute_name", "attribute_value"); 

只需更換* ATTRIBUTE_NAME *和* ATTRIBUTE_VALUE *與任何你需要的。

0

試試這個

$("#jqChart>canvas:eq(1)").css("top", "1px"); 

或者,

$("#jqChart>canvas").eq(1).css("top", "1px"); 

注意,在eq(n),n是從零開始的。與nth-child備選asolution可能看起來像

$("#jqChart>canvas:nth-child(2)").css("top", "1px"); 
0

可以選擇與eq方法第二畫布元件(其指定索引處返回元素,從0開始,從匹配組元素):

var canvas = $("#jqChart canvas").eq(1); 

我不完全確定你的意思是top屬性。我想你可能會參照top CSS屬性,在這種情況下你想要的css方法:

canvas.css("top", "100px"); 

如果實際上指的是top屬性,然後看attr方法。

您也可以使用:eq選擇器,但這比方法.eq慢。

2
$("#jqChart > canvas:nth-child(2)").attr('top', 'yourValue'); 
0

在jQuery中,你可以用

$('#jqChart > canvas:last').attr('top', 'attr_value');