Javascript 在mouseover上调用Js函数

Javascript 在mouseover上调用Js函数,javascript,html,onmouseover,Javascript,Html,Onmouseover,有没有办法在HTML中通过鼠标调用Javascript函数 我有以下代码: <script src="navSound.js" type="text/javascript"></script> <a href="#" onmouseover="bubble2.playclip()"><li class="navBackground"><div class="navButton">Gallery</div></li&g

有没有办法在HTML中通过鼠标调用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文档的外部文件中


有什么想法吗?

是的,可能是这样的:

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

函数playclip(){
警报(‘播放剪辑…’);
}
在您的例子中,playclip()函数将位于外部文件“navSound.js”中,该文件包含在使用“src”属性的脚本标记中

更新:

我更新了您的小提琴,您可以在此处看到效果良好的更改:

(当心:你会听到马的嘶嘶声;)

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

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

window.onload=函数(){
var html5_audiotypes={//定义音频文件扩展名及其关联音频类型的列表。如果指定的音频文件不在此列表中,请添加到该列表:
“mp3”:“音频/mpeg”,
“mp4”:“音频/mp4”,
“ogg”:“音频/ogg”,
“wav”:“音频/wav”
}
函数createsoundbite(声音){
var html5audio=document.createElement('audio');
if(html5audio.canPlayType){//检查对html5audio的支持
对于(var i=0;i

是的,可能是这样的:

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

函数playclip(){
警报(‘播放剪辑…’);
}
在您的例子中,playclip()函数将位于外部文件“navSound.js”中,该文件包含在使用“src”属性的脚本标记中

更新:

我更新了您的小提琴,您可以在此处看到效果良好的更改:

(当心:你会听到马的嘶嘶声;)

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

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

window.onload=函数(){
var html5_audiotypes={//定义音频文件扩展名及其关联音频类型的列表。如果指定的音频文件不在此列表中,请添加到该列表:
“mp3”:“音频/mpeg”,
“mp4”:“音频/mp4”,
“ogg”:“音频/ogg”,
“wav”:“音频/wav”
}
函数createsoundbite(声音){
var html5audio=document.createElement('audio');
if(html5audio.canPlayType){//检查对html5audio的支持
对于(var i=0;i

是的,可能是这样的:

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

函数playclip(){
警报(‘播放剪辑…’);
}
在您的例子中,playclip()函数将位于外部文件“navSound.js”中,该文件包含在使用“src”属性的脚本标记中

更新:

我更新了您的小提琴,您可以在此处看到效果良好的更改:

(当心:你会听到马的嘶嘶声;)

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

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

window.onload=函数(){
var html5_audiotypes={//定义音频文件扩展名及其关联音频类型的列表。如果指定的音频文件不在此列表中,请添加到该列表:
“mp3”:“音频/mpeg”,
“mp4”:“音频/mp4”,
“ogg”:“音频/ogg”,
“wav”:“音频/wav”
}
函数createsoundbite(声音){
var html5audio=document.createElement('audio');
if(html5audio.canPlayType){//检查对html5audio的支持
对于(var i=0;i

是的,可能是这样的:

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

函数playclip(){
警报(‘播放剪辑…’);
}
在您的例子中,playclip()函数将位于外部文件“navSound.js”中,该文件包含在使用“src”属性的脚本标记中

更新:

我更新了您的小提琴,您可以在此处看到效果良好的更改:

(当心:你会听到马的嘶嘶声;)

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

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

window.onload=函数(){
var html5_audiotypes={//定义音频文件扩展名及其关联音频类型的列表。如果指定的音频文件不在此列表中,请添加到该列表:
“mp3”:“音频/mpeg”,
“mp4”:“音频/mp4”,
“ogg”:“音频/ogg”,
“wav”:“音频/wav”
}
函数createsoundbite(声音){
var html5audio=document.createElement('audio');
if(html5audio.canPlayType){//检查对html5audio的支持
对于(var i=0;i

可能是一个愚蠢的问题,但您是否正确导入了外部脚本?您是否可以在不使用鼠标盖的情况下调用该函数,例如在页面加载或其他情况下?而且我相信您不需要()afterplayclip。不要认为这会伤害你,尽管你的控制台日志中有任何错误吗?
F12
在任何浏览器中。为什么有链接?它不在任何地方,也无效。感谢你的回复。此评论应该解决你的所有问题:我在我的浏览器控制台日志中没有看到任何错误或警告。现在链接故意不指向任何地方,因为一旦我使用新的navigaiton完成原型页面,我将在href中添加文件名。我相信脚本已正确导入,下面是我的导入声明:可能是愚蠢的问题,但您是否正确导入了外部脚本?您可以在不使用鼠标盖的情况下调用函数吗,like在页面加载还是什么?我相信你不需要()afterplayclip。不要认为这会伤害你,尽管你的控制台日志中有任何错误吗?
F12
在任何浏览器中。为什么有链接?它不在任何地方,也无效。感谢你的回复。此评论应该解决你的所有问题:我在我的浏览器控制台日志中没有看到任何错误或警告。现在链接故意不指向任何地方,因为一旦我使用新的navigaiton完成原型页面,我将在href中添加文件名。我相信脚本已正确导入,下面是我的导入声明:可能是愚蠢的问题,但您是否正确导入了外部脚本?您可以在不使用鼠标盖的情况下调用函数吗,like在页面加载还是什么?我相信你不会