【问题标题】:Adding attribute to second child in div将属性添加到div中的第二个孩子
【发布时间】:2012-01-14 03:17:31
【问题描述】:
谁能告诉我如何将“top”属性添加到第二个“canvas”标签中,该标签在 div 中没有 id,并且适用于使用 jquery 或其他任何东西的 IE。以下是 HTML sn-p。
<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>
【问题讨论】:
标签:
jquery
html
css
internet-explorer
【解决方案1】:
$("#jqChart > canvas:nth-child(2)").attr('top', 'yourValue');
【解决方案2】:
你可以用这个:
$("#jqChart > canvas:nth-child(2)").attr("attribute_name", "attribute_value");
只需将 *attribute_name* 和 *attribute_value* 替换为您需要的任何内容。
【解决方案3】:
试试这个
$("#jqChart>canvas:eq(1)").css("top", "1px");
或者,
$("#jqChart>canvas").eq(1).css("top", "1px");
请注意,在eq(n) 中,n 是从零开始的。 nth-child 的替代解决方案可能看起来像
$("#jqChart>canvas:nth-child(2)").css("top", "1px");
【解决方案4】:
您可以使用eq 方法选择第二个画布元素(该方法从匹配的元素集中返回指定索引处的元素,从0开始):
var canvas = $("#jqChart canvas").eq(1);
我不完全确定 top 属性是什么意思。我想您可能指的是top CSS 属性,在这种情况下您需要css 方法:
canvas.css("top", "100px");
如果您确实指的是top 属性,请查看attr 方法。
您也可以使用:eq 选择器,但这比.eq 方法慢。
【解决方案5】:
在 jQuery 中,您可以使用
$('#jqChart > canvas:last').attr('top', 'attr_value');