开发者

Executing a jquery function to all href attributes

function openFile(file) 开发者_如何学运维{
    var extension = file.substr( (file.lastIndexOf('.') +1) );
    switch(extension) {
        case 'jpg':
        case 'png':
        case 'gif':
            alert('was jpg png gif');  
        break;                         
        case 'zip':
        case 'rar':
            alert('was zip rar');
        break;
        case 'pdf':
            alert('was pdf');
        break;
        default:
            alert('who knows');
    }
};

openFile("somestring.png");

I got this piece a code from another question posted here, but im unsure how to implement it for my purpose. I want to check the hrefs of every link and place the appropriate icon for the file type.


Use .each function:

$("a").each(function() { openFile($(this).attr('href')); }

It allows you to iterate through the matched sets of elements.


$('a').each(function () {
    openFile($(this).attr('href'));
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜