[英]Jquery on off first click doesn't fire on first click
我正在使用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>
}
你有尝试过吗?
$(".song").on('click', function () {
$(this).off('click');
// do the other stuff
}
这样,您的链接可以被单击一次,然后单击事件处理程序将被删除。
在此,您首先使用点击功能,而不是使用关闭点击
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.