Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何将css类添加到目标以除.pdf以外的任何文件后缀结尾的链接中_Jquery - Fatal编程技术网

Jquery 如何将css类添加到目标以除.pdf以外的任何文件后缀结尾的链接中

Jquery 如何将css类添加到目标以除.pdf以外的任何文件后缀结尾的链接中,jquery,Jquery,我想将css类添加到一个链接中,其中目标以除.pdf或.jpg之外的任何文件后缀结尾。我想用jquery和filter来实现这一点。您可以用类似 $('a').filter(function (){return $(this).attr('target').indexOf('.pdf', $(this).attr('target').length - 4) === -1 && return $(this).attr('href').indexOf('.jpg', $(this).

我想将css类添加到一个链接中,其中目标以除.pdf或.jpg之外的任何文件后缀结尾。我想用jquery和filter来实现这一点。

您可以用类似

$('a').filter(function (){return $(this).attr('target').indexOf('.pdf', $(this).attr('target').length - 4) === -1 && return $(this).attr('href').indexOf('.jpg', $(this).attr('target').length - 4) === -1});

我得到一个错误代码:uncaughtypeerror:无法读取未定义的属性'indexOf'。当我将'target'更改为'href'并将'.length-3'更改为'.length-4'(因为'.pdf'中有4个字符)时,它会起作用。这里是整个代码$('a')。filter(函数(){return$(this).attr('href')。indexOf('.pdf',$(this)。attr('href')。length-4)===-1})您可能是对的,我没有测试我的代码。我将编辑我的答案以备将来参考