src、href、link的区别是什么?

src, href, and link are all attributes used in HTML, but they serve different purposes and behave differently, particularly concerning how they load resources and affect the current page. Here's a breakdown of their differences:

src (source):

  • Purpose: Embeds or imports the resource into the current document. Think of it as replacing the tag with the content of the resource.
  • Usage: Used for resources that are essential to the current document's structure and content. This includes images, videos, scripts, and iframes.
  • Resource Loading: The browser immediately fetches and loads the resource specified by the src attribute. This can block rendering of the page until the resource is loaded.
  • Example: <img src="image.jpg" alt="My Image">, <script src="script.js"></script>, <video src="video.mp4"></video>
  • Impact on Page: The resource becomes part of the current page. For example, with <script src="script.js"></script>, the JavaScript code in script.js is executed as part of the current page.

href (hypertext reference):

  • Purpose: Specifies the location (URL) of a resource or a different part of the same document. It creates a hyperlink.
  • Usage: Used for linking to other web pages, sections within the same page (using fragment identifiers like #section1), or other resources that are not directly embedded. It's the core attribute for the <a> (anchor) tag. Also used with <link> for stylesheets and other external resources.
  • Resource Loading: The browser doesn't immediately load the resource unless the user clicks the link or the browser prefetches it.
  • Example: <a href="https://www.example.com">Example Website</a>, <a href="#top">Back to Top</a>, <link rel="stylesheet" href="styles.css">
  • Impact on Page: Clicking a link with href typically navigates the user to the specified URL, potentially replacing the current page. With <link>, it instructs the browser to load and apply the linked resource (like a stylesheet) without changing the current page's content directly.

link (link element):

  • Purpose: Defines the relationship between the current document and an external resource. It's not a standalone attribute but an HTML element.
  • Usage: Primarily used for linking stylesheets, favicons, prefetching resources, and other relationships defined by the rel attribute. It always uses the href attribute to specify the URL of the linked resource.
  • Resource Loading: The browser loads the linked resource asynchronously, usually without blocking the rendering of the page. The rel attribute specifies the type of relationship, which influences how the browser handles the resource.
  • Example: <link rel="stylesheet" href="styles.css">, <link rel="icon" href="favicon.ico">, <link rel="prefetch" href="next-page.html">
  • Impact on Page: The linked resource affects the current page's presentation or behavior without being directly embedded in its content. For instance, a stylesheet linked with <link rel="stylesheet"> changes the page's styling.

In summary:

Feature src href link + href
Purpose Embeds resource Specifies a link/location Defines document relationship
Usage Images, scripts, video, etc. Hyperlinks, stylesheets Stylesheets, favicons, etc.
Loading Immediate, blocking On click/prefetch Asynchronous, non-blocking
Impact on Page Part of page content Navigation/resource applied Resource applied

Hopefully, this clarifies the distinctions between src, href, and link. They are crucial for building web pages and understanding how resources are loaded and used.

posted @ 2024-11-23 08:24  王铁柱6  阅读(56)  评论(0)    收藏  举报