简体   繁体   English

href javascript函数调用

[英]href javascript function call

I am trying to call a function on href but its showing undefined function but the function itself is working when I call it inside album loop. 我试图在href上调用一个函数,但显示的是未定义的函数,但是当我在专辑循环内调用该函数时,该函数本身正在运行。 Is there some other way of calling the function after appending the href? 追加href后还有其他方法可以调用该函数吗? I tried using jquery onclick with the id of anchor but that doesn't work either and got no error. 我尝试使用带有锚ID的jquery onclick,但这也不起作用,也没有错误。 Any idea how I should be calling? 知道我应该怎么打电话吗?

function getPhotosByAlbumID(albumID){
    FB.api('/'+ albumID + '/photos',{ limit: 5000, fields: 'source' }, function(photos) {
        $(photos.data).each(function(index, value){
            console.log(value.source);
        });
    });
}

FB.api('/me/albums',{ limit: 5000, fields: 'id,name,count,cover_photo,link' }, function(me_album) {
    $(me_album.data).each(function(index, value){                           
        FB.api('/'+value.id+'/picture',{ limit: 5000 }, function(picture) {                     
            $('#facebook-cover').append('<li id="'+value.id+'"><a href="javascript:getPhotosByAlbumID('+value.id+');" id="album"><img src="'+picture.data.url+'" /></a></li>'); 
        });
    });
}); 

Error: Uncaught ReferenceError: getPhotosByAlbumID is not defined 错误:未捕获ReferenceError:未定义getPhotosByAlbumID

All appended like so.. 所有都这样附上..

<ul id="facebook-cover">
<li id="4758611198834"><a href="javascript:getPhotosByAlbumID(4758611198834);" id="album"><img src="image-link"></a></li>   
</ul>

try 尝试

var li=$('<li id="'+value.id+'"></li>');
var anchor=$('<a href="#" id="album"></a>');
anchor.click(function(){
getPhotosByAlbumID(value.id);
return false;

});
var img=$('<img src="'+picture.data.url+'" />');

anchor.append(img);
li.append(anchor);

this way your click event must work. 这样,您的点击事件必须有效。 unless there is any error inside getPhotosByAlbumID(); 除非getPhotosByAlbumID()内部没有任何错误; you can also return false from the same function 您也可以从同一函数返回false

My guess would be that the js-file is not properly included in the page where the html is. 我的猜测是js文件未正确包含在html所在的页面中。 One easy way of testing if this is the problem would be to just put the javascript in the same file as your links. 测试这是否是问题的一种简单方法是将javascript与链接放在同一文件中。 If this solves the problem, you have an include error. 如果这样可以解决问题,则包含错误。

Like so: 像这样:

<script type="text/javascript">
function getPhotosByAlbumID(albumID){
    FB.api('/'+ albumID + '/photos',{ limit: 5000, fields: 'source' }, function(photos) {
        $(photos.data).each(function(index, value){
            console.log(value.source);
        });
    });
}

FB.api('/me/albums',{ limit: 5000, fields: 'id,name,count,cover_photo,link' }, function(me_album) {
    $(me_album.data).each(function(index, value){                           
        FB.api('/'+value.id+'/picture',{ limit: 5000 }, function(picture) {                     
            $('#facebook-cover').append('<li id="'+value.id+'"><a href="javascript:getPhotosByAlbumID('+value.id+');" id="album"><img src="'+picture.data.url+'" /></a></li>');
        });
    });
});
</script>

<ul id="facebook-cover">
<li id="4758611198834"><a href="javascript:getPhotosByAlbumID(4758611198834);" id="album"><img src="image-link"></a></li>   
</ul> 

I notice you originally had a significant amount of indentation in your JS code. 我注意到您最初在JS代码中有大量缩进。 Is your getPhotosByAlbumID function within some other function perhaps? 您的getPhotosByAlbumID函数是否在其他函数中? Something like this: 像这样:

var module = (function () {
    ...
    function getPhotosByAlbumID(albumID){
    ...
}

If so you need to make it accessible. 如果是这样,则需要使其可访问。 Here is some info on the module pattern which may be useful. 这是有关模块模式的一些信息 ,可能有用。

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

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