Distinguish between DOM attributes and element attributes
Distinguish between DOM attributes and element attributes
An img tag:
##<img src="//m.sbmmt.com/global/code/images/image.1.jpg" id="hibiscus" alt=" Hibiscus" class="classA" />
Usually developers are accustomed to calling id, src, alt, etc. the "attributes" of this element. We call this "element attribute". However, when parsing into a DOM object, the actual browser will eventually parse the tag element into a "DOM object" and store the element's "element attributes" as "DOM attributes". There is a difference between the two. Although we set the src of the element to be a relative path: images/image.1.jpg
But it will all appear in the "DOM Attributes" Convert to an absolute path: http://localhost/images/image.1.jpg
Even the names of some "element attributes" and "DOM attributes" are It's different. For example, the element attribute class above is converted into a DOM attribute and corresponds to className. Keep in mind that in javascript we can directly get or set "DOM attributes":<script type="text/javascript">
So if you want to set the CSS style class of an element, you need to use the DOM attribute "className" instead of the element attribute "class: $(function( ) { var img1 = document.getElementById("hibiscus");
alert(img1.alt);
img1.alt = "Change the alt element attribute";
alert(img1.alt);
})</script>
alert(img1.alt);
img1.alt = "Change the alt element attribute";
alert(img1.alt);
})</script>
img1.className = "classB";
There is no wrapper function for operating "DOM properties" in jQuery, because it is very simple to use javascript to get and set "DOM properties". Provided in jQuery The each() function is used to traverse the jQuery package set, where the this pointer is a DOM object, so we can apply this with native javascript to operate the DOM attributes of the element: $("img").each(function(index) {
alert("index:" + index + ", id:" + this.id + ", alt:" + this.alt);
this.alt = "changed";
alert("index:" + index + ", id:" + this.id + ", alt:" + this.alt);
});The following is a description of the each function : each( callback ) Returns: jQuery wrapper set
new file
<!DOCTYPE html>
<html>
<body>
<p id="intro">Hello World!</p>
<script>
x=document.getElementById("intro");
document.write(x.firstChild.nodeValue);
</script>
</body>
</html>
Preview
Clear
- Course Recommendations
- Courseware download
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~
Students who have watched this course are also learning
Let's briefly talk about starting a business in PHP
Quick introduction to web front-end development
Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
Login verification and classic message board
Computer network knowledge collection
Quick Start Node.JS Full Version
The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)
















