简体   繁体   English

div上方的隐藏按钮

[英]Hidden button above div

My goal is to hide a transparent button above a div element. 我的目标是在div元素上方隐藏一个透明按钮。 When a user clicks the button, properties of the underlying div should be changed using code in a C# click event , not client-based code. 用户单击按钮时,应使用C#click事件中的代码(而不是基于客户端的代码)更改基础div的属性。

  • The code below provides an example of changing a div's color properties, but my actual application modifies different properties. 下面的代码提供了一个更改div的颜色属性的示例,但是我的实际应用程序修改了不同的属性。
  • The button is clickable when positioned beside the div, but not when hidden above it (I tried using z-index). 当位于div旁边时,该按钮是可单击的,但是当隐藏在其上方时,该按钮不是可单击的(我尝试使用z-index)。
  • I understand that using JQuery or a dedicated CSS class is recommended above overwriting CSS styles, but I am required to use this approach. 我了解建议在覆盖CSS样式之前使用JQuery或专用CSS类,但是我必须使用这种方法。

Are there any additional CSS properties that could make the hidden button clickable? 是否还有其他CSS属性可以使隐藏按钮可点击?

CSS: CSS:

#container {
    height:200px;
    width:200px;
    position:relative;
    z-index:auto;
}

#MyHiddenButton {
    float:left;
    height:100px;
    width:200px;
    z-index:2;
    background-color:transparent;
    position: absolute;
    top: 0;
    outline: none;
    border: none;
}

#myContent {
    position:absolute;
    float:left;
    height:200px;
    width:200px;
    background-color:lightslategrey;
    z-index:1;
}

HTML: HTML:

<div id="container">
    <div id="myContent" runat="server"></div>
    <asp:Button ID="MyHiddenButton" runat="server" OnClick="MyHiddenButton_Click" />
</div>

C# Code: C#代码:

protected void MyHiddenButton_Click(object sender, EventArgs e)
{
    myContent.Style.Add("background-color", "red");
}

If I'm understanding your problem correctly, a div isn't considered "clickable" in ASP, so you're trying to get a button to overlap and handle a click event in C#. 如果我正确地理解了您的问题,则ASP中的div不被认为是“可点击的”,因此您正在尝试使按钮重叠并处理C#中的click事件。 If this is correct, you could handle the problem this way . 如果这是正确的,则可以通过这种方式解决问题。

Also, I got it working using purely front end code like so: 另外,我使用纯前端代码使它工作,如下所示:

 #container { position: relative; height: 200px; width: 200px; } #myHiddenButton { position: absolute; top: 0; height: 200px; width: 200px; outline: none; border: none; background-color: transparent; z-index: 2; } #myContent { position: absolute; height: 200px; width: 200px; background-color: lightslategrey; z-index: 1; } 
 <div id="container"> <div id="myContent"></div> <button id="myHiddenButton" onclick="alert('test');" /> </div> 

*Note: I changed the one id to be consistent capitalization. *注意:我将一个ID更改为大小写一致。 You may need to change the id if you copy/paste any CSS (Don't mind my OCD) 如果您复制/粘贴任何CSS,则可能需要更改ID(不要介意我的OCD)

In this instance, I think you're better off controlling the display logic, including click areas with basic html elements, then using jquery on clicks to fire the event. 在这种情况下,我认为最好控制显示逻辑,包括具有基本html元素的单击区域,然后在单击时使用jquery触发事件。 Within that click you can then do a postback for the button. 在该单击中,您可以对该按钮进行回发。 So the html would be something like this: 因此,HTML将是这样的:

<script>
    $( "#myContent" ).click(function() {
       __doPostBack('<%= MyHiddenButton.ClientID %>', '')
    });
</script>

<div id="container">
    <div id="myContent" runat="server"></div>    
</div>
<asp:Button ID="MyHiddenButton" runat="server" OnClick="MyHiddenButton_Click" style="display:none" />

The button is hidden, so the event can still fire, but the div is the element that is firing the event. 该按钮是隐藏的,因此事件仍然可以触发,但是div是触发事件的元素。 If you wanted just the top part of the div to be clickable you can create another div within it that is the click area. 如果您只想单击div的顶部,则可以在其中创建另一个div,即单击区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM