`

Manipulating element properties and attributes

阅读更多

1.Manipulating element properties

Command syntax:each(iterator)

Traverses all elements in the matched set invoking the passed iterator function for each.

Parameters

iterator (Function) A function called for each element in the matched set. The parameter passed to this function is set to the zero-based index of the element within the set, and the element itself is available as the this property of the function.

Returns

The wrapped set.

eg: $('img').each(function(n){

            this.alt='This is image['+n+'] with an id of '+this.id;

      });//This statement will invoke the inline function for each image element on the page, modifying its alt property using the order of the element and its id value. 

 

 

2.Fetching attribute values

 

Command syntax:attr(name)

Obtains the values assigned to the specified attribute for the first element in the matched set.

 

The attr() command can be used to either fetch the value of an attribute from the first element in the matched  set or set attribute values onto all matched elements.

 

Parameters

name (String) The name of the attribute whose value is to be fetched.

Returns

The value of the attribute for the first matched element. The value undefined is returned if 

the matched set is empty or the attribute doesn’t exist on the first element.

 

3.Setting attribute values

 

Command syntax:attr(name,value)

Sets the named attribute onto all elements in the wrapped set using the passed value.

Parameters

name (String) The name of the attribute to be set.

value (String|Object|Function) Specifies the value of the attribute. This can be any JavaScript expression that results in a value, or it can be a function. See the following 

discussion for how this parameter is handled.

Returns

The wrapped set.

eg: $('*').attr('title',function(index) {

            return 'I am element ' + index + ' and my name is ' +

           (this.id ? this.id : 'unset');

     });

     $("a[href^=http://]").attr("target","_blank");// set all the links open in a new window!

 

 

 

Command syntax:attr(attributes)

Sets the attributes and values specified by the passed object onto all elements of the matched set

Parameters

attributes (Object) An object whose properties are copied as attributes to all  elements in the wrapped set

Returns

The wrapped set

eg: $('input').attr(

           { value: '', title: 'Please enter a value' }

     );

 

 

note: Internet Explorer won’t allow the name attribute of <input> elements to 

be changed. If you want to change the name of <input> elements in 

Internet Explorer, you must replace the element with a new element pos-

sessing the desired name.

 

4.Removing attributes

 

Command syntax:removeAttr(name)

Removes the specified attribute from every matched element

Parameters

name (String) The name of the attribute to be removed

Returns

The wrapped set

 

 

 

 

 

分享到:
评论

相关推荐

    scipylectures

    - **Numerical Operations on Arrays:** Performing arithmetic operations, statistical computations, and element-wise operations. - **More Elaborate Arrays:** Creating arrays with specific properties, ...

    Django 1.0 Website Development.pdf

    Accessing CSS properties and HTML attributes 102 Manipulating HTML documents 103 Traversing the document tree 103 Handling events 104 Sending AJAX requests 105 What next? 105 Implementing live ...

    Servlets和JSP核心技术 卷2(英文版) 第一部分

    Defining the Header and the Root Element Section 2.3. The Elements of web.xml Section 2.4. Assigning Names and Custom URLs Section 2.5. Disabling the Invoker Servlet Section 2.6. Initializing and...

    Servlets和JSP核心技术 卷2(英文版) 第二部分

    Defining the Header and the Root Element Section 2.3. The Elements of web.xml Section 2.4. Assigning Names and Custom URLs Section 2.5. Disabling the Invoker Servlet Section 2.6. Initializing and...

    ZendFramework中文文档

    14.5.3. Retrieving Validated Fields and other Reports 14.5.3.1. Querying if the input is valid 14.5.3.2. Getting Invalid, Missing, or Unknown Fields 14.5.3.3. Getting Valid Fields 14.5.4. Using ...

Global site tag (gtag.js) - Google Analytics