SRC和HREF之间的区别 [英] Difference between SRC and HREF

查看:180
本文介绍了SRC和HREF之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SRC HREF 属性用于包含一些外部实体,如图像,CSS文件, HTML文件,任何其他网页或JavaScript文件。



SRC 和 HREF ?何时何地使用 SRC HREF ?我认为它们不能互换使用。



下面给出几个使用这些属性的示例:


  • 在链接标记内引用一个CSS文件: href =cssfile.css

  • 在脚本标记内引用JS文件: src =myscript.js
  • 引用图像文件: src =mypic.jpg图片标签内

  • 要引用另一个网页: href = http://www.webpage.com在锚标记内。


  • 解决方案 div>

    注意: @ John-Yin的答案更适合考虑规格。






    是的。 src href 存在区别,它们不能互换使用。我们使用 src 替换 元素元素 href 用于在引用文档和外部资源之间建立关系。
    $ b href (超文本参考)属性指定Web资源的位置,从而定义当前元素(在锚点 a 的情况下)或当前文档(在链接的情况下)之间的链接或关系)以及由此属性定义的目标锚点或资源。当我们写作时:

     < link href =style.css =stylesheet/> 

    浏览器明白这个资源是一个样式表和处理解析该页面<暂停> 暂停(由于浏览器需要样式规则来绘制和呈现页面,因此呈现可能会暂停)。这与将样式标签内的css文件的内容转储非常类似。 (因此,建议您使用 link 而不是 @import 将样式表附加到您的html文档中。)


    $ b src (Source)属性只是将资源嵌入到当前文档中元素定义位置。例如。当浏览器发现

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

    页面的加载和处理暂停,直到浏览器读取,编译和执行文件。它类似于在脚本标签内转储js文件的内容。类似的情况是 img 标签。它是一个空标签,内容应该放在里面,由 src 属性定义。浏览器暂停加载,直到它获取并加载图像。 [所以是 iframe的情况]



    这就是为什么建议将所有JavaScript文件加载到底部(在< / body> 标记之前)




    更新:请参阅@ John-Yin的回答以获取更多有关如何实施的信息根据HTML 5规范。


    The SRC and HREF attributes are used to include some external entities like an image, a CSS file, a HTML file, any other web page or a JavaScript file.

    Is there a clear differentiation between SRC and HREF? Where or when to use SRC or HREF? I think they can't be used interchangeably.

    I'm giving below few examples where these attributes are used:

    • To refer a CSS file: href="cssfile.css" inside the link tag.
    • To refer a JS file: src="myscript.js" inside the script tag.
    • To refer an image file: src="mypic.jpg" inside an image tag.
    • To refer another webpage: href="http://www.webpage.com" inside an anchor tag.

    解决方案

    NOTE: @John-Yin's answer is more appropriate considering the changes in the specs.


    Yes. There is a differentiation between src and href and they can't be used interchangeably. We use src 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 is not 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]

    This is the reason why it is advisable to load all JavaScript files at the bottom (before the </body> tag)


    update : Refer @John-Yin's answer for the more info on how it is implemented as per HTML 5 specs.

    这篇关于SRC和HREF之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆