Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.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
Javascript jquery attr(名称、值)方法';s值参数_Javascript_Jquery - Fatal编程技术网

Javascript jquery attr(名称、值)方法';s值参数

Javascript jquery attr(名称、值)方法';s值参数,javascript,jquery,Javascript,Jquery,我正在阅读Bibeault的jQuery,我很难理解attr()方法的值参数。这本书说,参数可以是一个函数,其参数是索引和先前的值。这些参数的用途是什么?我不明白课文的解释 我特别想知道: 这些参数是强制性的吗 如何使用这些参数的示例是什么 我可以在函数中使用其他参数吗 attr是访问元素属性的一种方法。允许函数的重载将允许您使用函数return设置值(函数的索引参数将是选择的索引,以前的值是它到现在为止的值) 我自己从来没有使用过这个重载,但假设您正在基于某种函数生成属性值,这会很好 它们不是

我正在阅读Bibeault的jQuery,我很难理解
attr()
方法的
参数。这本书说,参数可以是一个函数,其参数是
索引
先前的值
。这些参数的用途是什么?我不明白课文的解释

我特别想知道:

  • 这些参数是强制性的吗
  • 如何使用这些参数的示例是什么
  • 我可以在函数中使用其他参数吗

  • attr是访问元素属性的一种方法。允许函数的重载将允许您使用函数return设置值(函数的索引参数将是选择的索引,以前的值是它到现在为止的值)

    我自己从来没有使用过这个重载,但假设您正在基于某种函数生成属性值,这会很好

    它们不是强制性的,传递不同数量的参数可提供不同的功能:

    attr('name')-获取name的值

    attr('name','value')-设置name的值

    attr('name',函数(i,v){return v+i;});-将name的值设置为集合中的上一个值+索引

    例如: 假设我们有五个跨度,类为hiccup,名称为“是”

    $('span.hiccup').attr('name',function(i,v){return v + i;});
    
    将为每个跨度命名为“yup1”-“yup5”

    此外,您还可以在函数内部访问它,该函数引用元素本身。考虑到这一点,你可能会做一些非常有趣的事情

    与往常一样,jQuery提供了所有这些方面的精彩文档:

    attr是访问元素属性的一种方法。允许函数的重载将允许您使用函数return设置值(函数的索引参数将是选择的索引,以前的值是它到现在为止的值)

    我自己从来没有使用过这个重载,但假设您正在基于某种函数生成属性值,这会很好

    它们不是强制性的,传递不同数量的参数可提供不同的功能:

    attr('name')-获取name的值

    attr('name','value')-设置name的值

    attr('name',函数(i,v){return v+i;});-将name的值设置为集合中的上一个值+索引

    例如: 假设我们有五个跨度,类为hiccup,名称为“是”

    $('span.hiccup').attr('name',function(i,v){return v + i;});
    
    将为每个跨度命名为“yup1”-“yup5”

    此外,您还可以在函数内部访问它,该函数引用元素本身。考虑到这一点,你可能会做一些非常有趣的事情

    与往常一样,jQuery提供了所有这些方面的精彩文档:
    其实很简单。
    attr()
    函数有三种可能的模式;您所引用的函数接受回调以获取值

    例如:

     $('.someClass').attr('rel', function(index, value)
     {
          // index refers to the elements index of the set; so of all elements with the
          //    css class 'someClass', the index will refer to that position in the list.
          //    If three elements match, the callback will be invoked 3 times, with 0, 1, 2
          //    as the index when each element, respectively, is invoked. 
    
          // value refers to the current value of the attribute. 
    
          // Return the value you want to set.
          return 'SomeRelValue';
     });
    
     $('.someElements').attr('rel', function(index, value)
     {
          return value + index;
     });
    
    参数不是强制性的;如果您只是从回调签名中省略它们,那么您就无法访问该信息。不能将其他参数传递给此方法。当您要匹配许多元素,并希望根据选择器元素的顺序位置插入一些数据时,可能需要使用此函数

    例如:

     $('.someClass').attr('rel', function(index, value)
     {
          // index refers to the elements index of the set; so of all elements with the
          //    css class 'someClass', the index will refer to that position in the list.
          //    If three elements match, the callback will be invoked 3 times, with 0, 1, 2
          //    as the index when each element, respectively, is invoked. 
    
          // value refers to the current value of the attribute. 
    
          // Return the value you want to set.
          return 'SomeRelValue';
     });
    
     $('.someElements').attr('rel', function(index, value)
     {
          return value + index;
     });
    

    对于与选择器匹配的每个元素,可以将rel属性设置为原来的值加上选择器的索引。因此,如果元素1的rel为'sampleRel',则设置为'sampleRel1',元素2的rel为'sampleRel',则变为'sampleRel2',以此类推,实际上非常简单。
    attr()
    函数有三种可能的模式;您所引用的函数接受回调以获取值

    例如:

     $('.someClass').attr('rel', function(index, value)
     {
          // index refers to the elements index of the set; so of all elements with the
          //    css class 'someClass', the index will refer to that position in the list.
          //    If three elements match, the callback will be invoked 3 times, with 0, 1, 2
          //    as the index when each element, respectively, is invoked. 
    
          // value refers to the current value of the attribute. 
    
          // Return the value you want to set.
          return 'SomeRelValue';
     });
    
     $('.someElements').attr('rel', function(index, value)
     {
          return value + index;
     });
    
    参数不是强制性的;如果您只是从回调签名中省略它们,那么您就无法访问该信息。不能将其他参数传递给此方法。当您要匹配许多元素,并希望根据选择器元素的顺序位置插入一些数据时,可能需要使用此函数

    例如:

     $('.someClass').attr('rel', function(index, value)
     {
          // index refers to the elements index of the set; so of all elements with the
          //    css class 'someClass', the index will refer to that position in the list.
          //    If three elements match, the callback will be invoked 3 times, with 0, 1, 2
          //    as the index when each element, respectively, is invoked. 
    
          // value refers to the current value of the attribute. 
    
          // Return the value you want to set.
          return 'SomeRelValue';
     });
    
     $('.someElements').attr('rel', function(index, value)
     {
          return value + index;
     });
    
    对于与选择器匹配的每个元素,可以将rel属性设置为原来的值加上选择器的索引。因此,如果元素1的rel为'sampleRel',则将其设置为'sampleRel1',带有rel'sampleRel'的元素2变为'sampleRel2',以此类推。

    1)javascript中没有强制参数。你想用多少就用多少。这些参数可在函数中使用

    2) 示例:

    假设您有以下html:

    <a href="#" title="Google"></a>
    <a href="#" title="Yahoo"></a>
    <a href="#" title="Bing"></a>
    
    这将在每个标题的末尾添加字符串“-外部链接”

    现在,看看这个:

    $('a').attr('title', function(index, previousValue){
        return previousValue + ' - Link number ' + index;
    });
    
    这将产生以下html:

    <a href="#" title="Google - Link number 0"></a>
    <a href="#" title="Yahoo - Link number 1"></a>
    <a href="#" title="Bing - Link number 2"></a>
    
    1) javascript中没有强制参数。你想用多少就用多少。这些参数可在函数中使用

    2) 示例:

    假设您有以下html:

    <a href="#" title="Google"></a>
    <a href="#" title="Yahoo"></a>
    <a href="#" title="Bing"></a>
    
    这将在每个标题的末尾添加字符串“-外部链接”

    现在,看看这个:

    $('a').attr('title', function(index, previousValue){
        return previousValue + ' - Link number ' + index;
    });
    
    这将产生以下html:

    <a href="#" title="Google - Link number 0"></a>
    <a href="#" title="Yahoo - Link number 1"></a>
    <a href="#" title="Bing - Link number 2"></a>
    

    jQuery中有一个很好的例子,我将在这里缩短它:

    以下是相关的HTML:

    <div>Zero-th </div>
    <div>First </div>
    <div>Second </div>
    
    $
    函数返回所有div,因此
    arr
    参数允许您基于索引指定属性值


    传递给attr的函数还接收到一个指定旧属性值的值,但由于这是javascript,因此该函数不必命名该参数,并且它仍然可以在
    参数中使用,在jQuery中有一个很好的示例,我将在这里缩短它:

    以下是相关的HTML:

    <div>Zero-th </div>
    <div>First </div>
    <div>Second </div>
    
    $
    函数返回所有div,因此
    arr
    参数允许您基于索引指定属性值

    传递给attr的函数还接收到一个指定旧属性值的值,但由于这是javascript,因此该函数不会