[英]Jquery on off first click doesn't fire on first click
I'm using MVC 5 and Razor front end. 我正在使用MVC 5和Razor前端。 When I click any button for the first time, it will not play.
当我第一次单击任何按钮时,它将不会播放。 It will play on the second click.
它将在第二次单击时播放。 I tried .off at the end of the code but it didnt work.
我在代码末尾尝试了.off,但是没有用。 I want it to play for the first click on a button then turn off.
我希望它先播放然后单击关闭。 Right now it takes two clicks on a button to start the track and work correctly for the other tracks.
现在,只需单击两次按钮即可启动轨道并针对其他轨道正常工作。
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;
}
});
Here is my HTML: 这是我的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>
}
Have you tried this? 你有尝试过吗?
$(".song").on('click', function () {
$(this).off('click');
// do the other stuff
}
This way your link can be clicked once, and then click event handler is removed. 这样,您的链接可以被单击一次,然后单击事件处理程序将被删除。
在此,您首先使用点击功能,而不是使用关闭点击
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.