选中悬停查询的循环中的定位标记文本

Check Anchor Tag Text in a Loop for HoverJquery

本文关键字:文本 定位 循环 查询 悬停      更新时间:2023-09-26

如果文本的值在悬停时为"Joined",我会被困在需要更改文本值的位置,但我不知道为什么它总是试图继续,否则这意味着它是错误的,

这是我的Jquery

<script type="text/javascript">
    $('.k-button').hover(
    function () {
        var $this = $(this);
        if ($this.text() == "Joined") {
            $this.data('initialText', $this.text());
            $this.text("Unjoin?");
        } else {
            $this.text($this.data('initialText'));
        }
    },
    function () {
        var $this = $(this);
        $this.text($this.data('initialText'));
    }
);
</script>

<td>@if (@item.IsJoined == "Joined")
                        {
                    <a href="/User/UnjoinEvent?EventId=@item.EventId&eventdateid=@item.EventDateId" id="btnDelete+@item.EventDateId" class="k-button" style="text-decoration: none;">
                             @item.IsJoined</a>
                        }
                        else
                        {
                             <a href="/User/JoinEvent?eventId=@item.EventId&eventdateid=@item.EventDateId" id="btnEdits+@item.EventDateId" class="k-button" style="text-decoration: none; ">
                            @item.IsJoined</a>
                        }
                    </td>

此Html代码位于Table/GridView 中

感谢能帮助我的人

您忘记了修剪文本。修复方法如下:

 <a href="#" class="k-button" style="text-decoration: none;">
                             Joined</a>
<script type="text/javascript">
    $('.k-button').hover(
    function () {
        var $this = $(this);
        if ($this.text().trim() == "Joined") {
            $this.data('initialText', $this.text());
            $this.text("Unjoin?");
        } else {
            $this.text($this.data('initialText'));
        }
    },
    function () {
        var $this = $(this);
        $this.text($this.data('initialText'));
    }
);
</script>

演示http://jsfiddle.net/87RXZ/

不确定这是否有效:如果您正在动态生成"td":

$('a').on('hover','.k-button',function () {
        var $this = $(this);
        if ($this.text().trim() == "Joined") {
            $this.data('initialText', $this.text());
            $this.text("Unjoin?");
        } else {
            $this.text($this.data('initialText'));
        }
    }
);

使用

$(this).text().trim()=="已加入"

$(this).html().trim()="已加入"

您应该使用这些代码

jQuery( ".class" ).on({
    mouseenter:
    function()
    {
    //Some code
    },
    mouseleave:
    function()
    {
    //Some code
    }
});

这些代码将适用于您使用的多个标记,而悬停仅适用于第一个标记演示:http://jsfiddle.net/kapil_dev/tLRX4/1/