简体   繁体   English

按钮隐藏在鼠标悬停在图像上

[英]Button hide on mouseover on image

i have the following code: 我有以下代码:

  <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

<script src="Js/jquery.min.js"></script>

<script type="text/javascript">
        $(document).ready(function () {
            $("#propertyimage").mouseover(function () {

                $("#lnkedit").hide();
            });
            $("#propertyimage").mouseout(function () {
                $("#lnkedit").show();
            });

        });
    </script>


<div class="ddldemo">
  <asp:Repeater ID="rptproperty" runat="server">
   <ItemTemplate>
      <div style="width: 165px;">
          <asp:Image ID="propertyimage" runat="server" ImageUrl='<%#Eval("image1") %>'" />
          <asp:LinkButton runat="server" ID="lnkedit" Text="Edit"></asp:LinkButton>
       </div>
   </ItemTemplate>
   </asp:Repeater>
 </div>
 </asp:Content>

all code is in content place holder when i take mouse over to the image button is not hide/show . 当我将鼠标移到图像按钮上时,所有代码都位于内容占位符中,不是隐藏/显示。

how can i solve this? 我该如何解决?

The issue is your element doesn't have an ID. 问题是您的元素没有ID。 The ID attribute in your code is the server side ASP.NET ID, it's not the HTML ID attribute. 您代码中的ID属性是服务器端ASP.NET ID,而不是HTML ID属性。 For the client side ID use ClientID : 对于客户端ID,请使用ClientID

<asp:Image ID="propertyimage" ClientID="propertyimage" runat="server" ImageUrl='<%#Eval("image1") %>'" />

Same with your LinkButton: 与您的LinkBut​​ton相同:

<asp:LinkButton runat="server" ID="lnkedit" ClientID="lnkedit" Text="Edit"></asp:LinkButton>

When debugging code like this, remember to view the source in the browser, to see the actual HTML that the browser is receiving. 在调试这样的代码时,请记住在浏览器中查看源代码,以查看浏览器正在接收的实际HTML。

If you have multiple of these, then a fixed ID is not going to work but it must be unique. 如果您有多个,则固定ID无效,但必须唯一。 In that case, you will need to use something else such as a class, but you'll also need to add logic to get the button relative to the image. 在这种情况下,您将需要使用诸如类之类的其他东西,但是您还需要添加逻辑以获取相对于图像的按钮。

Be careful, I don't know much about ASP, but I see "template" and "repeater"? 请注意,我对ASP不太了解,但是我看到“模板”和“中继器”吗? So there seems to be a good chance for you to create several inputs with the same ID. 因此,您似乎很有可能用相同的ID创建多个输入。

Try this : 尝试这个 :

<script src="Js/jquery.min.js"></script>

<script type="text/javascript">
        $(document).ready(function () {
            $("#propertyimage").mouseover(function () {

                $(this).next().hide();
            });
            $("#propertyimage").mouseout(function () {
                $(this).next().show();
            });

        });
    </script>


<div class="ddldemo">
  <asp:Repeater ID="rptproperty" runat="server">
   <ItemTemplate>
      <div style="width: 165px;">
          <asp:Image class="propertyimage" runat="server" ImageUrl='<%#Eval("image1") %>'" />
          <asp:LinkButton runat="server" class="lnkedit" Text="Edit"></asp:LinkButton>
       </div>
   </ItemTemplate>
   </asp:Repeater>
 </div>

You can't do that when you have controls in an ItemTemplate. 当ItemTemplate中有控件时,您将无法执行此操作。 The unique client id of the control is not going to be #propertyimage. 控件的唯一客户端ID不会是#propertyimage。 It can't be since you could have multiple user controls with the same button name. 不能这样,因为您可以有多个具有相同按钮名称的用户控件。 ASP.Net will generate a unique id that is relevant to the control tree. ASP.Net将生成与控件树相关的唯一ID。 In this case getting the clientid is not going to work since you are in a repeater control and you can't hav emultiple controls with the same client id. 在这种情况下,由于您处于中继器控件中,因此无法使用相同的客户端ID来拥有多个控件,因此无法获取clientid。

You can do this with classes though. 您可以通过类来做到这一点。 Here is an example I put together on jsFiddle . 这是我在jsFiddle上放在一起的示例 It uses a class to identify the repeater, then uses the jquery next() method to select the next anchor element and show/hide it. 它使用一个类来标识转发器,然后使用jquery的next()方法选择下一个锚元素并显示/隐藏它。 Try altering your code like so: 尝试像这样更改代码:

<script src="Js/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".propertyImage").mouseover(function () {

            $(this).next("a").hide();
        });
        $(".propertyImage").mouseout(function () {
            $(this).next("a").show();
        });

    });
</script>
<div class="ddldemo">
<asp:Repeater ID="rptproperty" runat="server">
<ItemTemplate>
  <div style="width: 165px;">
      <asp:Image ID="propertyimage" class="propertyImage" runat="server" ImageUrl='<%#Eval("image1") %>'" />
      <asp:LinkButton runat="server" ID="lnkedit" Text="Edit"></asp:LinkButton>
   </div>
</ItemTemplate>
</asp:Repeater>
</div>

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

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