【问题标题】:Jquery on off first click doesn't fire on first click第一次单击时关闭的 Jquery 不会在第一次单击时触发
【发布时间】:2018-06-30 01:25:09
【问题描述】:

我正在使用 MVC 5 和 Razor 前端。当我第一次单击任何按钮时,它不会播放。它将在第二次单击时播放。我在代码末尾尝试了 .off ,但它没有用。我希望它在第一次单击按钮时播放,然后关闭。现在只需点击两次按钮即可启动轨道并正常运行其他轨道。

function play() {
//$(".song").unbind().click(function () {
$(".song").off('click').on('click', function () {

        //this is the url
        var url = $(this).attr("data-song");

    var audio = $("#myAudio")[0];

    if (audio.paused) {
        audio.src = url;
        audio.play();
    } else {
        audio.pause();
        audio.currentTime = 0;
    }


});

这是我的 HTML:

<table class="table" id="trackTable">
<tr>
    <th>

    </th>
    <th>
        @Html.DisplayNameFor(model => model.Title)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Genre)
    </th>

</tr>

@foreach (var item in Model)
{
    <tr>
        <td class="songRow">
            <a href="#"  class="song" data-song="@Url.Content(item.Path)"  onclick="play()" ><img src="~/Images/playpause.png" alt="playpause" id="playbutton"   /></a>

        </td>

        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Genre)
        </td>
        <td></td>
    </tr>
}

【问题讨论】:

    标签: jquery asp.net-mvc razor


    【解决方案1】:

    你试过了吗?

    $(".song").on('click', function () {
       $(this).off('click');
    
       // do the other stuff
    }
    

    这样你的链接可以被点击一次,然后 click 事件处理程序被删除。

    【讨论】:

    • 试过了,根本不会播放。
    【解决方案2】:

    在这个你首先使用点击功能而不是使用关闭点击

    【讨论】:

    • 仍然需要点击两下才能播放。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 2018-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    相关资源
    最近更新 更多