我在 asp:Repeater 中有一个 div:
<ItemTemplate>
<div id="myDiv" style="display:none" />
</ItemTemplate>
</asp:Repeater>
<script type="text/javascript">
window.onload = function() {
document.getElementById('myDiv').style.display = 'block';
</script>
这很好用,除了我的 div 元素出现在转发器中,这意味着只找到第一个 div。有人可以向我解释如何获取 ItemTemplate 中的所有 div 吗?
最佳答案
同一个id在一个页面中多次使用是无效的。相反,您必须使用其他一些方法来查找您的元素,例如 css 类。 jQuery 使这项任务变得简单。
$(".someClass").show();
但是,您甚至可能不需要这样做。如果您要做的只是更改公共(public)父级下一组元素的某些样式,则只需更改父级的类名即可。考虑以下中继器:
<div id="repeaterParent">
<asp:Repeater runat="server">
<ItemTemplate>
<div class="repeaterItemDiv"></div>
</ItemTemplate>
</asp:Repeater>
</div>
像这样设置你的 CSS:
#repeaterParent .repeaterItemDiv
{
display: none;
}
#repeaterParent.showDivs .repeaterItemDiv
{
display: block;
}
然后使用这个 JavaScript:
document.getElementById("repeaterParent").className = "showDivs";
或者,这个 jQuery:
$("#repeaterItemParent").addClass("showDivs");
关于c# - 如何在 asp :Repeater 中为标签使用 getElementbyId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6182142/