onMouseOver/onMouseOut 在子元素上触发
这是我的基本代码:
<script type="text/javascript">
function play(id){
$("#player"+id).jPlayer("play", 0);
$("#jp-play"+id).css("background-color", "#999999");
$("#jp-pause"+id).css("background-color", "#999999");
$("#jp-play"+id).css("background-image", "url(images/voice_over/hover.png)");
$("#jp-pause"+id).css("background-image", "url(images/voice_over/hover.png)");
}
function end(id){
$("#player"+id).jPlayer("stop");
$("#jp-play"+id).css("background-color", "#656565");
$("#jp-pause"+id).css("background-color", "#656565");
$("#jp-play"+id).css("background-image", "url(images/voice_over/normal.png)");
$("#jp-pause"+id).css("background-image", "url(images/voice_over/normal.png)");
}
</script>
<div class="jp-audio" onMouseOver="play(\''.$id.'\');" onMouseOut="end(\''.$id.'\');">
//child elements, id from PHP
</div>
当 div 悬停在上面时播放音频。 有几个 div(每个 div 播放不同的音频)。
但是将鼠标悬停在每个 div 中的子元素(如文本)上将会停止音频。
我知道这与冒泡/传播有关。 我正在使用 jQuery,只是尝试添加 event.stopPropagtion();到功能,但它不起作用。
其他解决方案需要使用 .hover() 等。 那么我如何获得要播放的文件的 id 呢?
我希望这是有道理的。 我发现很难解释我的意思。
任何帮助将不胜感激。
现在我的工作代码是:
var play = function() {
var id = $(this).attr('file-id');
$("#player"+id).jPlayer("play", 0);
$("#jp-play"+id).css("background-color", "#999999");
$("#jp-pause"+id).css("background-color", "#999999");
$("#jp-play"+id).css("background-image", "url(images/voice_over/hover.png)");
$("#jp-pause"+id).css("background-image", "url(images/voice_over/hover.png)");
}
var end = function() {
var id = $(this).attr('file-id');
$("#player"+id).jPlayer("stop");
$("#jp-play"+id).css("background-color", "#656565");
$("#jp-pause"+id).css("background-color", "#656565");
$("#jp-play"+id).css("background-image", "url(images/voice_over/normal.png)");
$("#jp-pause"+id).css("background-image", "url(images/voice_over/normal.png)");
}
$(document).ready(function(){
$('.jp-audio').hover(play, end);
});
<div class="jp-audio" file-id="'.$id.'">stuff</div>
在 .hover 之前添加变量非常重要,特别是当根据用户输入动态加载内容时。
我用另一种方式解决了这个问题,当使用innerHTML时它不起作用。
This is my basic code:
<script type="text/javascript">
function play(id){
$("#player"+id).jPlayer("play", 0);
$("#jp-play"+id).css("background-color", "#999999");
$("#jp-pause"+id).css("background-color", "#999999");
$("#jp-play"+id).css("background-image", "url(images/voice_over/hover.png)");
$("#jp-pause"+id).css("background-image", "url(images/voice_over/hover.png)");
}
function end(id){
$("#player"+id).jPlayer("stop");
$("#jp-play"+id).css("background-color", "#656565");
$("#jp-pause"+id).css("background-color", "#656565");
$("#jp-play"+id).css("background-image", "url(images/voice_over/normal.png)");
$("#jp-pause"+id).css("background-image", "url(images/voice_over/normal.png)");
}
</script>
<div class="jp-audio" onMouseOver="play(\''.$id.'\');" onMouseOut="end(\''.$id.'\');">
//child elements, id from PHP
</div>
This plays audio when the div is hovered over.
There are several divs (that each play different audio).
But hovering over child elements in each div (like text) will stop the audio.
I understand that this is something to do with bubbling/propagation.
I am using jQuery and simply tried adding event.stopPropagtion(); to the functions but it didn't work.
Other solutions require using .hover() etc.
But then how would I get the id of which file to play?
I hope that makes sense.
I'm finding it hard to explain what I mean.
Any help would be appreciated.
Now my working code is:
var play = function() {
var id = $(this).attr('file-id');
$("#player"+id).jPlayer("play", 0);
$("#jp-play"+id).css("background-color", "#999999");
$("#jp-pause"+id).css("background-color", "#999999");
$("#jp-play"+id).css("background-image", "url(images/voice_over/hover.png)");
$("#jp-pause"+id).css("background-image", "url(images/voice_over/hover.png)");
}
var end = function() {
var id = $(this).attr('file-id');
$("#player"+id).jPlayer("stop");
$("#jp-play"+id).css("background-color", "#656565");
$("#jp-pause"+id).css("background-color", "#656565");
$("#jp-play"+id).css("background-image", "url(images/voice_over/normal.png)");
$("#jp-pause"+id).css("background-image", "url(images/voice_over/normal.png)");
}
$(document).ready(function(){
$('.jp-audio').hover(play, end);
});
<div class="jp-audio" file-id="'.$id.'">stuff</div>
It's important to have the variables before the .hover, especially when the content is dynamically loaded based on user input.
I had it around the other way and when using innerHTML it would not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更好的方法是将 $id 设置为 html 元素的 attr
A better way to do it is set $id to be a attr of the html element