简体   繁体   English

li onClick功能不起作用

[英]li onClick function not working

I have the following files in the order it stands: 我按其顺序有以下文件:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript" src="js/music.js"></script>

in main.js I have the following code: 在main.js中,我有以下代码:

$(document).ready(function () {
       $('.song').click(function() {
        $('.footer').show('easeInExpo');
        audioPlayer(band, album, track);
    });
});

In music.js where the audioPlayer function is called: 在music.js中,调用audioPlayer function

function audioPlayer(band, album, track) {
 var audio = new Audio; //Class
audio.src = 'music/' + band + '/' + album + '/' + track + '.mp3';
audio.play();
}

The code that calls the function: 调用该函数的代码:

<li class="song" onlick="javascript:audioPlayer('Band name', 'Album name', 'Track name');"></li>

If I run this code I get: Uncaught ReferenceError: band is not defined 如果运行此代码, Uncaught ReferenceError: band is not defined得到: Uncaught ReferenceError: band is not defined

I have also tried to do the following with main.js: 我也尝试过对main.js执行以下操作:

$('.song').click(function(band, album, track) {
    $('.footer').show('easeInExpo');
    audioPlayer(band, album, track);
});

Which gives me: GET http://localhost/media/music/[object%20Object]/undefined/undefined.mp3 404 (Not Found) 这给了我: GET http://localhost/media/music/[object%20Object]/undefined/undefined.mp3 404 (Not Found)

Does anyone knows how to fix the audioPlayer() function problem? 有谁知道如何解决audioPlayer() function问题? Please let me know if you need more information. 如果您需要更多信息,请告诉我。 Thanks in advance 提前致谢

The onlick="javascript:audioPlayer('Band name', 'Album name', 'Track name');" onlick="javascript:audioPlayer('Band name', 'Album name', 'Track name');" code within the li is set up to provide these parameters, but it's not spelled correctly onlick should be onclick . li内的代码设置为提供这些参数,但是拼写不正确onlick应该是onclick

And there is also a click event listener which is executed outside of that onclick function so it doesn't get any parameters. 还有一个单击事件监听器,该监听器在该onclick函数外部执行,因此它不获取任何参数。


One solution would be to change your li's to use data-attributes: 一种解决方案是更改您的li以使用数据属性:

<li class="song" data-band="Band name" data-album="Album name" data-track="Track name"></li>

then modify the click listener as follows: 然后按如下所示修改点击监听器:

$('.song').click(function() {
    $('.footer').show('easeInExpo');
    var data = $(this).data();
    audioPlayer(data.band, data.album, data.track);
});

Remove the onClick listener on the li tag. 除去li标签上的onClick侦听器。 Instead, in the jQuery, find the band, album, and track names and call the function. 相反,在jQuery中,找到乐队,专辑和曲目名称,然后调用该函数。 Something like this: 像这样:

$('.song').click(function(band, album, track) {
$('.footer').show('easeInExpo');

var band = $(this).data("band");
var album = $(this).data("album");
var track = $(this).data("track");
audioPlayer(band, album, track);
});

And add the data attributes on the li: 并在li上添加数据属性:

<li class="song" data-band="test band" data-album="test album" data-track="test track"></li>

It looks like your problem with with the OnClick function: 看来您在使用OnClick函数时遇到了问题:

Please try to change : 请尝试更改:

<li class="song" onlick="javascript:audioPlayer('Band name', 'Album name', 'Track name');"></li>

To: 至:

  <li class="song" onclick="audioPlayer(\'' + bandname + ','+ albumName +','+ Track + \')"></li>

I am not sure if this is the only problem, but if it is you should really be doing this with proper DOM methods though. 我不确定这是否是唯一的问题,但是如果确实是这样,那么您确实应该使用适当的DOM方法来做到这一点。

var liElement = document.createElement('li');
liElement.addClass("song");
liElement.addEventListener('click', function(){
    audioPlayer(song.band,song.name,song.track);
});

​document.body.appendChild(liElement);​

Here, your 在这里,你的

<li class="song" onlick="javascript:audioPlayer('Band name', 'Album name', 'Track name');"></li>

not works, reason is spelling mistake. 不起作用,原因是拼写错误。

$(document).ready(function () {
   $('.song').click(function() {
    $('.footer').show('easeInExpo');
    audioPlayer(band, album, track);
});});

will not work, because you are not specified what are band, album, track in the scope. 将无法使用,因为您没有在范围内指定乐队,专辑,曲目。 You will not require the jquery part if you are use 'onclick' in your li tag. 如果在li标记中使用“ onclick”,则不需要jquery部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM