如何使用 jquery 保留所选 asp 按钮的 css 类?

发布于 2024-11-27 23:43:29 字数 1698 浏览 0 评论 0原文

我在一个页面上有一些按钮,大约 10 个 asp 按钮。我正在使用 CSS 类来实现按钮鼠标悬停和鼠标离开的效果。我面临的问题是,当我单击按钮时,我想将 CSS 类应用于单击的按钮并保留它,但是当我将鼠标悬停在该按钮上时,该类被删除。实际上,我想禁用所选(单击)按钮的类更改。因此,如果我有 5 个按钮 btn1、btn2、btn3、btn4、btn5 并且我选择的按钮是 btn3,如果我将鼠标悬停在 btn3 上并且留下这个 btn3 ,它不应该丢失它的 css 类。

这是我应用 CSS 类的代码:

 <script src="jquery-1.6.2.js" type="text/javascript"></script>
    <script type="text/javascript">

        $(document).ready(function () {

            // mouse hover
            $("[id^='MangoPager_btn']").mouseenter(mouseenterfunction);
            // mouse leave
            $("[id^='MangoPager_btn']").mouseleave(mouseleavefunction);           

        });

        function mouseleavefunction() {

            $(this).removeClass("pagerbtnMouseEnter").addClass("buttonclass");
        }

        function mouseenterfunction() {

            $(this).addClass("pagerbtnMouseEnter");
        }

    </script>

以及单击按钮时使用的代码:

     private void SetSelectedButtonStyle()
            {
ResetCss():
                //selectedItemClass 
                Button selectedButton = FindButtonWithText(_currentPageIndex.ToString());
                if (selectedButton != null)
                {
                    selectedButton.CssClass = "pagerbtnMouseEnter";
                }
            }

 private void ResetCss()
        {            

            for (int i = 1; i <= MAX_PAGE_SIZE; i++)
            {
                Button btn = (Button)FindControl(string.Format("btn{0}", i));
                btn.CssClass = "buttonclass";
            }
        }

当我进行鼠标悬停或鼠标离开时,我必须防止所选按钮更改 CSS 类。

I have some buttons on a page, around 10 asp buttons. I'm using a CSS class for effects on button mouse hover and mouse leave. The problem I'm facing is that when I click a button, I want to apply a CSS class to the clicked button and want to keep it, but when I hover on that button the class is removed. Actually, I want to kind of disable the change of class for selected (clicked) button. So, if I have 5 buttons btn1, btn2, btn3, btn4, btn5 and my selected button is btn3, if I hover on btn3 and leave this btn3 , it should not loose its css class.

This is my code for applying CSS classes:

 <script src="jquery-1.6.2.js" type="text/javascript"></script>
    <script type="text/javascript">

        $(document).ready(function () {

            // mouse hover
            $("[id^='MangoPager_btn']").mouseenter(mouseenterfunction);
            // mouse leave
            $("[id^='MangoPager_btn']").mouseleave(mouseleavefunction);           

        });

        function mouseleavefunction() {

            $(this).removeClass("pagerbtnMouseEnter").addClass("buttonclass");
        }

        function mouseenterfunction() {

            $(this).addClass("pagerbtnMouseEnter");
        }

    </script>

and the code I'm using when a button is clicked:

     private void SetSelectedButtonStyle()
            {
ResetCss():
                //selectedItemClass 
                Button selectedButton = FindButtonWithText(_currentPageIndex.ToString());
                if (selectedButton != null)
                {
                    selectedButton.CssClass = "pagerbtnMouseEnter";
                }
            }

 private void ResetCss()
        {            

            for (int i = 1; i <= MAX_PAGE_SIZE; i++)
            {
                Button btn = (Button)FindControl(string.Format("btn{0}", i));
                btn.CssClass = "buttonclass";
            }
        }

I have to prevent the selected button from changing the CSS class when I do a mouse hover or mouse leave.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感受沵的脚步 2024-12-04 23:43:29

You 按钮有 3 种不同的状态:
- 普通的
- 悬停
- 已选

为什么你只有2节课?如果你想这样做,你应该有另一个 css 类用于按下状态,然后在你的悬停功能上,你将检查你的按钮是否具有选定的类,如果有则不执行任何操作。

但是你难道不知道悬停状态只能通过CSS实现吗?

.myClass
{
  background-color : blue;
}

.myClass:hover
{
  background-color : red;
}

每当您将鼠标悬停在 myclass 元素上时,这都会改变它的背景颜色。

因此,您可以做的就是删除 mouseenter / mouseleave 的所有 jquery 代码,

为单击状态创建一个新类。

.buttonClass
{
   // Your button style
}
.buttonClass:hover
{
   // Your mousein button style 
}
.buttonClicked
{
   // Your selected/clicked button style
}

在您的 asp.net 页面上,只需将单击的按钮的 .button 类替换为 buttonClicked 类:)

You button has 3 different states :
- Normal
- Hovering
- Selected

Why do you only have 2 classes? If you wanna do that way you should have another css class for the pressed state, and then on your hovering functions you'll check if your button has the selected class , and if it has then do nothing.

But don't you know that the hovering state is possible by css only?

.myClass
{
  background-color : blue;
}

.myClass:hover
{
  background-color : red;
}

This will change the background color of a myclass element whenever your hover it.

So what you can do is delete all your jquery code for the mouseenter / mouseleave

Create a new class for the clicked state.

.buttonClass
{
   // Your button style
}
.buttonClass:hover
{
   // Your mousein button style 
}
.buttonClicked
{
   // Your selected/clicked button style
}

And on your asp.net page juste replace the .button class of the clicked button with the buttonClicked class :)

小清晰的声音 2024-12-04 23:43:29

那么,您可以将单击的 Button 的 id 保存在对象中:

var clickedButtons = {};

$("[id^='MangoPager_btn']").click(function(){
    clickedButtons[this.id] = true;
});

然后修改 mouseleave 以检查按钮是否已被单击,仅在未单击时才删除该类:

    function mouseleavefunction() {
         if (clickedButtons[this.id] !== true){
            $(this).removeClass("pagerbtnMouseEnter").addClass("buttonclass");
          }

    }

Well you could save the id of the Button that is clicked in an object:

var clickedButtons = {};

$("[id^='MangoPager_btn']").click(function(){
    clickedButtons[this.id] = true;
});

Then modify mouseleave to check if the button has been clicked and remove the class only if it hasn't been clicked:

    function mouseleavefunction() {
         if (clickedButtons[this.id] !== true){
            $(this).removeClass("pagerbtnMouseEnter").addClass("buttonclass");
          }

    }
若能看破又如何 2024-12-04 23:43:29

这样的事情对你有用吗?


$(document).ready(function(){
    $("[id^='MangoPager_btn']").live(function() {
       click: function() {
          $(this).addClass("clickClass");
       },
       mouseover: function() {
          $(this).addClass("pagerbtnMouseEnter");
       },
       mouseout: function() {
          $(this).removeClass("pagerbtnMouseEnter")
          $(this).addClass("clickClass");
       }
    });
});

Would something like this work for you?


$(document).ready(function(){
    $("[id^='MangoPager_btn']").live(function() {
       click: function() {
          $(this).addClass("clickClass");
       },
       mouseover: function() {
          $(this).addClass("pagerbtnMouseEnter");
       },
       mouseout: function() {
          $(this).removeClass("pagerbtnMouseEnter")
          $(this).addClass("clickClass");
       }
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文