我有兴趣使用“onmouseover”事件使列表框显示并消失。我对ASP.NET很新,我还不想写javascript。我试图使用以下代码,它的颜色更改部分工作,但列表框可见性不起作用:
if(!IsPostBack) { Button2.Attributes.Add(“onmouseover”,“this.style.backgroundColor ='Red',ListBox3.style.visibility ='visible'”); }
if (!IsPostBack)
{
Button2.Attributes.Add("onmouseout", "this.style.backgroundColor='Blue', ListBox3.style.visibility='hidden'");
}
我尝试过这个代码有没有“PostBack”,但仍然没有运气。有谁看到我的代码在哪里失败了?
谢谢,
DFM
答案 0 :(得分:2)
尝试:
if (!IsPostBack)
{
btnHide.Attributes.Add("onmouseout", "this.style.backgroundColor='Blue';ListBox3.style.display='none'");
btnShow.Attributes.Add("onmouseover", "this.style.backgroundColor='Red';ListBox3.style.display='block'");
}
visibility属性的工作方式与display属性略有不同。当visibility属性设置为'hidden'时,元素被隐藏但布局不受影响,而将display属性设置为'none'则会完全删除元素,这可能会影响布局。
如果您希望修改列表的可见性而影响布局,则可以使用div作为包装,然后修改其可见性属性。
<div id="wrapper">
<asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>
</div>
<asp:Button ID="btnShow" runat="server" Text="Button" />
<asp:Button ID="btnHide" runat="server" Text="Button" />
修改ASPX以切换包含列表框的div元素的visibility属性。
if (!IsPostBack)
{
btnHide.Attributes.Add("onmouseout", "this.style.backgroundColor='Blue';wrapper.style.visibility='hidden'");
btnShow.Attributes.Add("onmouseover", "this.style.backgroundColor='Red'; wrapper.style.visibility='visible'");
}