Jquery on off first click不';不要第一次点击就开火

Jquery on off first click不';不要第一次点击就开火,jquery,asp.net-mvc,razor,Jquery,Asp.net Mvc,Razor,我正在使用MVC5和Razor前端。当我第一次单击任何按钮时,它都不会播放。它将在第二次单击时播放。我试过了。在代码末尾关闭了,但它不起作用。我想让它播放第一次点击按钮,然后关闭。现在,只需点击两次按钮即可启动该曲目,并正确运行其他曲目 function play() { //$(".song").unbind().click(function () { $(".song").off('click').on('click', function () { //this is th

我正在使用MVC5和Razor前端。当我第一次单击任何按钮时,它都不会播放。它将在第二次单击时播放。我试过了。在代码末尾关闭了,但它不起作用。我想让它播放第一次点击按钮,然后关闭。现在,只需点击两次按钮即可启动该曲目,并正确运行其他曲目

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>
}

@DisplayNameFor(model=>model.Title)
@DisplayNameFor(model=>model.Genre)
@foreach(模型中的var项目)
{
@DisplayFor(modeleItem=>item.Title)
@DisplayFor(modeleItem=>item.Genre)
}
你试过这个吗

$(".song").on('click', function () {
   $(this).off('click');

   // do the other stuff
}

这样,您的链接可以单击一次,然后单击事件处理程序将被删除。

在此u中,首次使用“单击打开”功能而不是“单击关闭”

仍然需要单击两次才能播放。尝试过之后,它将完全不播放。