首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用Js函数onMouseOver

问调用Js函数onMouseOver
EN

Stack Overflow用户
提问于 2014-04-18 14:32:42
回答 1查看 7.8K关注 0票数 0

有没有办法从HTML中的鼠标调用Javascript函数?

我有以下代码:

代码语言:javascript
复制
<script src="navSound.js" type="text/javascript"></script>

<a href="#" onmouseover="bubble2.playclip()"><li class="navBackground"><div class="navButton">Gallery</div></li></a>

我希望它调用一个名为"playclip()“的函数,该函数位于链接到名为navSound.js的HTML的外部文件中。

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-18 14:40:16

是的,有可能是这样的:

代码语言:javascript
复制
<html>
<head>

<script type='text/javascript'>

function playclip() {
    alert('playing clip...');
}

</script>

</head>
<body>

<a href="#" onmouseover="playclip()"><li class="navBackground"><div class="navButton">Gallery</div></li></a>

</body>
</html>

在您的示例中,播放剪辑()函数将使用'src‘属性在脚本标记中包含的外部文件"navSound.js“中。

更新:

我更新了您的Fiddle,您可以在这里看到这些更改,它们运行得很好:

http://jsfiddle.net/Lb966/ (小心:你会听到马的嘶嘶声;)

我还在粘贴下面的标记+代码:

代码语言:javascript
复制
<html>
<head>
<script type='text/javascript'>

window.onload = function() {

    var html5_audiotypes={ //define list of audio file extensions and their associated audio types. Add to it if your specified audio file isn't on this list:
        "mp3": "audio/mpeg",
        "mp4": "audio/mp4",
        "ogg": "audio/ogg",
        "wav": "audio/wav"
    }

    function createsoundbite(sound){
        var html5audio=document.createElement('audio');
        if (html5audio.canPlayType){ //check support for HTML5 audio
            for (var i=0; i<arguments.length; i++){
                var sourceel=document.createElement('source')
                sourceel.setAttribute('src', arguments[i])
                if (arguments[i].match(/\.(\w+)$/i))
                    sourceel.setAttribute('type', html5_audiotypes[RegExp.$1])
                html5audio.appendChild(sourceel)
            }
            html5audio.load()
            html5audio.playclip=function(){
                //html5audio.pause()
                html5audio.currentTime=0
                html5audio.play()
            }
            return html5audio
        }
        else{
            return {playclip:function(){throw new Error("Your browser doesn't support HTML5 audio unfortunately")}}
        }
    }

    var audio = createsoundbite("http://www.w3schools.com/html/horse.ogg");
    audio.setAttribute('id', 'myAudio');
    document.getElementById('content').appendChild(audio);

}

</script>
</head>
<body>
<div id='content'></div>
<a href="#" onmouseover="document.getElementById('myAudio').play()">Gallery</a>
</body>
</html>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23156233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档