我在html中嵌入了vlcplayer,想要播放,暂停视频。所以我创建了一个javascript文件并编写了一些函数
<head>
<script type="text/javascript" src="jquery-1.7.1.min.js" ></script>
<script type="text/javascript" src="vctrl.js" ></script>
</head>
<body>
<embed id="vlcp" type="application/x-vlc-plugin" name="VLC" autoplay="no" loop="no" volume="100" width="640" height="480" target="test.flv">
</embed>
<a href="#" onclick='play()'>Play</a>
<a href="#" onclick='pause()'>Pause</a>
</body>
javascript文件有
$(document).ready(function(){
var player = document.getElementById("vlcp");
var play = function(){
if(player){
alert("play");
}
};
var pause = function(){
if(player){
alert("pause");
}
};
}
);
当我点击播放链接时,警告框不会出现..我的onclick
值是否错误?
答案 0 :(得分:2)
您的函数play
和pause
被定义为您为ready
函数提供的函数的局部变量。所以它们对DOM对象不可见。
解决方案可能是以通常的方式(或window.play = function...
)声明它们。
但是使用jquery的正确方法是使用jquery绑定函数:
<head>
<script type="text/javascript" src="jquery-1.7.1.min.js" ></script>
<script type="text/javascript" src="vctrl.js" ></script>
</head>
<body>
<embed id="vlcp" type="application/x-vlc-plugin" name="VLC" autoplay="no" loop="no" volume="100" width="640" height="480" target="test.flv">
</embed>
<a id=playbutton href="#">Play</a>
<a id=pausebutton href="#">Pause</a>
</body>
$(document).ready(function(){
var player = document.getElementById("vlcp");
$('#playbutton').click(function(){
if(player){
alert("play");
}
});
$('#pausebutton').click(function(){
if(player){
alert("pause");
}
});
}
);
答案 1 :(得分:0)
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="vctrl.js" ></script>
</head>
<body>
<embed id="vlcp" type="application/x-vlc-plugin" name="VLC" autoplay="no" loop="no" volume="100" width="640" height="480" target="test.flv">
</embed>
<a href="javascript:void(0);" class="play" onclick='play()'>Play</a>
<a href="javascript:void(0);" class="play" onclick='pause()'>Pause</a>
</body>
<script type="text/javascript">
$(document).ready(function(){
var player = document.getElementById("vlcp");
$('.play').click(function(){
var thisvalue = $(this).html();
if(thisvalue=="Play"){
alert("Play");
}else{
if(thisvalue=="Pause"){
alert("Pause");
}
}
});
});
</script>