object.src和object.getAttribute('src')之间的区别 [英] Difference between object.src and object.getAttribute('src')

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

问题描述

因此,这是一个示例HTML代码:

So, here is an example HTML code:

<img src="test.png" id="test">

这是一个Javascript代码:

And here is a Javascript code:

element = document.getElementById('test');
alert(element.getAttribute('src')); --> test.png
alert(element.src); --> domain.com/test.png

为什么getAttribute不显示域,而.src是,它将添加域?在哪里可以找到在DOM对象中访问属性的不同方式之间的区别?

Why would getAttribute not show the domain, while .src yes, it adds the domain? Where can I find the difference between the different ways of accessing attributes in a DOM object?

推荐答案

您可以看到区别

You can see the difference in the HTML specification as "permitted attributes" versus "DOM interface".

如果反映IDL属性是其内容属性定义为包含URL的 DOMString 属性,则在获取时,IDL属性必须解析content属性相对于元素的值并返回得到的绝对URL(如果成功),否则为空字符串;并且在设置时,必须将content属性设置为指定的文字值.如果content属性不存在,则IDL属性必须返回默认值(如果content属性为1,否则为空字符串).

If a reflecting IDL attribute is a DOMString attribute whose content attribute is defined to contain a URL, then on getting, the IDL attribute must resolve the value of the content attribute relative to the element and return the resulting absolute URL if that was successful, or the empty string otherwise; and on setting, must set the content attribute to the specified literal value. If the content attribute is absent, the IDL attribute must return the default value, if the content attribute has one, or else the empty string.

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

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