码迷,mamicode.com
首页 > 其他好文 > 详细

src和herf的区别

时间:2016-08-17 13:50:25      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:

src是链接,属于整体替换(替换非叠加),比如img标签和frame标签;href是超文本引用,属于附属资源,可以叠加。
再从html,css和js的关系来思考一下。html相当于人的身体(结构),css相当于衣服(样式),js相当于动作(行为)。想象一个人的生活,一件衬衫(css)可以搭配不同的外套(即叠加href),而不是说买了一件衬衫(css)以后,就不穿任何一件外套了(即非整体替换src)。再比如说,小时候不会用筷子吃饭(行为js),需要父母喂(行为),而自己学会吃饭这一行为以后,需要父母喂这一行为就被完全替换掉了(即src)。
 

There is a differentiation between src and href and they can‘t be used interchangeably. We usesrc for replaced elements while href for establishing a relationship between the referencing document and an external resource.

href (Hypertext Reference) attribute specifies the location of a Web resource thus defining a link or relationship between the current element (in case of anchor a) or current document (in case of link) and the destination anchor or resource defined by this attribute. When we write:

<link href="style.css" rel="stylesheet" />

The browser understands that this resource is a stylesheet and the processing parsing of the page isnot paused (rendering might be paused since the browser needs the style rules to paint and render the page). It is not similar to dumping the contents of the css file inside the style tag. (Hence is is advisable to use link rather than @import for attaching stylesheets to your html document.)

src (Source) attribute just embeds the resource in the current document at the location of the element‘s definition. For eg. When the browser finds

<script src="script.js"></script>

The loading and processing of the page is paused until this the browser fetches, compiles and executes the file. It is similar to dumping the contents of the js file inside the script tag. Similar is the case with img tag. It is an empty tag and the content, that should come inside it, is defined by the src attribute. The browser pauses the loading until it fetches and loads the image. [so is the case with iframe]


Stack Overflow :html - Difference between SRC and HREF

src和herf的区别

标签:

原文地址:http://www.cnblogs.com/lshblogs/p/5779479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!