image.setAttribute和image.src之间的区别 [英] Difference between image.setAttribute and image.src

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

问题描述

以下两种方法之间是否存在差异

Is there any difference between the below two methods

var image = document.createElement("img");
1) image.setAttribute('src','mySrc');
2) image.src = 'mySrc';

对于任何html元素,以上方式设置某些属性是否有区别?是否有任何浏览器可靠性?

For any html element, is there a difference in setting some attribute in the above ways ? Is there any browser dependability ?

推荐答案

使用setAttribute(),您可以添加属性,即使它不存在。
但它仅在主流浏览器上受支持。 Ie8和更早版本不支持
支持此功能。

With setAttribute() you can add an attribute even if it doesn't exist. But it's supported only on major browsers. Ie8 and earlier doesn't support this function.

来自w3schools

你应该使用该属性。

setAttribute的性能更差

我测试了100个图像元素。

I tested 100 image elements.


  • 运行一个更改src花了2毫秒的函数。

  • 使用setAttribute需要4毫秒。

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

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