Operations on inherent attributes of jquery elements: prop() and removeProp()

无忌哥哥
Release: 2018-06-29 11:49:38
Original
1631 people have browsed it

Operations of the inherent attributes of jquery elements: prop() and removeProp()

    2.元素固有属性的操作:prop()和removeProp() 
美女
Copy after login

1.prop(): Only the inherent attributes of the element can be obtained

Get the inherent attributes alt, title

var res = $('#pic').prop('alt') var res = $('#pic').prop('title')
Copy after login

Get the custom attribute data-nation, return undefined, and cannot get it

var res = $('#pic').prop('data-nation')
Copy after login
Copy after login

But how to use prop() to dynamically set the custom attribute, you can get it normally

var res = $('#pic').prop('data-nation','中国山东')
Copy after login

Looking at the element at this time, it is found that the custom attribute has not changed, so this setting has no impact on the element.

The custom attribute value viewed again at this time is only a temporary value that exists in the current script. Data

var res = $('#pic').prop('data-nation')
Copy after login
Copy after login

2.removeProp()

This method is different from removeAttr() in two points:

1. It does not support space-separated attribute list strings, that is, once Only one attribute can be deleted

2. It cannot delete native attributes. If you really want to delete it, just set the value to false

You cannot remove multiple attributes at the same time, so this statement Invalid

var res = $('#pic').removeProp('alt data-nation')
Copy after login

Delete the custom attribute data-nation

var res = $('#pic').removeProp('data-nation')
Copy after login

Delete the native attribute alt, the deletion failed

var res = $('#pic').removeProp('alt')
Copy after login

Use removeAttr() to delete the native attribute alt, the deletion was successful

var res = $('#pic').removeAttr('alt')
Copy after login

So, if you want to use removeProp() to delete native properties, in most cases you can set the value to false

Ultimately, it will be processed by the user script

var res = $('#pic').prop('alt',false) var res = $('#pic').prop('alt')
Copy after login

Console query Result

console.log(res)
Copy after login

The above is the detailed content of Operations on inherent attributes of jquery elements: prop() and removeProp(). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!