简体   繁体   English

为什么在这种情况下addClass不能识别为函数?

[英]Why is addClass not recognised as a function in this case?

I'm trying to add a class to a bunch of links whose suffix ends in .pdf as follows: 我试图将一类添加到其后缀以.pdf结尾的一堆链接中,如下所示:

var pdfs = jQuery("a[href$='.pdf']"); //Choosing .pdf links
pdfs.each(addClass("pdf")); // Adding the class

This doesn't work, however, as the browser console prints this error when I try to reload the HTML file calling the .js file: 但是,这不起作用,因为当我尝试重新加载调用.js文件的HTML文件时,浏览器控制台会显示此错误:

ReferenceError: addClass is not defined ReferenceError:未定义addClass

Why is this happening? 为什么会这样呢?

jQuery allows you to perform it's API methods not only on single elements, but also on collections of elements (which is one of the nice advantages over the native DOM API). jQuery允许您不仅在单个元素上而且在元素集合上执行其API方法(这是与本机DOM API相比的一大优点)。

The easiest way to achieve your goal is to capitalize on that: 实现目标的最简单方法是:

var pdfs = jQuery("a[href$='.pdf']"); //Choosing .pdf links
pdfs.addClass("pdf"); // Adding the class

jQuery $.each() function is for those cases where you want to do something with each element in the collection that is not available as an API method in jQuery. jQuery $.each()函数适用于您要对集合中的每个元素(而不是jQuery中的API方法)可用的情况进行处理的情况。 It expects you pass it a function that gets passed the current element in each iteration as this : 它期望您向其传递一个函数,该函数将在每次迭代中传递当前元素,如下this

var pdfs = jQuery("a[href$='.pdf']"); //Choosing .pdf links
pdfs.each(function() { console.log(this.href); }) // perform something jQuery doesn't offer as an API method

If you insist on using $.each() , the code is this: 如果您坚持使用$.each() ,则代码如下:

var pdfs = jQuery("a[href$='.pdf']"); //Choosing .pdf links
pdfs.each(function() { $(this).addClass("pdf"); }); // Adding the class

https://api.jquery.com/addClass/ https://api.jquery.com/addClass/

After choosing the pdf links you can directly call the addClass method on the variable pdfs because this is a jquery object and the class will be applied to all the links. 选择pdf链接后,您可以直接在变量pdfs上调用addClass方法,因为这是一个jquery对象,并且该类将应用于所有链接。

 var pdfs = jQuery("a[href$='.pdf']"); //Choosing .pdf links pdfs.addClass("pdf"); // Adding the class 

addClass(className) is a method of DOM element object. addClass(className)是DOM元素对象的方法。 Must be called with element object. 必须与元素对象一起调用。 when we call addClass() without DOM object reference it look for local function, and thus throw the reference error. 当我们在没有DOM对象引用的情况下调用addClass() ,它将查找局部函数,从而引发引用错误。

var pdfs = jQuery("a[href$='.pdf']"); 
pdfs.each(function() {
  $(this).addClass( "foo" );
});

So... in whole: 所以...整体而言:

$("a[href$='.pdf']").each(function () {
   $(this).addClass("pdf");
})

.each needs a function ( delegate ) for calling it for per item of jQuery result, but func() (in this case: addClass(..) ) will call addClass function (that is not defined in current scope) and pass result of calling it to each function. .each需要一个函数( delegate )来为每个jQuery结果项目调用它,但是func() (在这种情况下: addClass(..) )将调用addClass函数(在当前作用域中未定义)并传递结果调用 each函数。

you can access to addClass function of per item, like this: 您可以访问每个项目的addClass函数,如下所示:

pdfs.each(function(index, pdf){
   $(pdf).addClass(...);
});

or for all: 或全部:

pdfs.addClass(...);

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

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