-1

我执行

document.getElementsByTagName('img')[0]['!abc'] = "abc";

将“!abc”属性设置为一个图像元素。

当我尝试在内容脚本中获取此属性时,结果为空。

但是当我尝试在文档控制台中获取它时,结果是“abc”。

4

2 回答 2

1

使用 setAttribute();

var myImg = document.getElementsByTagName('img')[0];
myImg.setAtrribute("attrib_name", value);
于 2013-06-08T09:20:03.583 回答
0

您可以定义一个类似的函数:

function setElementAttrbute(tagname,index,attrName,value){

    var elements = document.getElementsByTagName(tagname); // returns kind of array
    elements[index].setAttribute(attrName,value); 

}

当你想使用像 (slice,push,pop,shift,unshift) 这样的数组方法时,你可以这样做:

document.getElementsByTagName(tagname); // returns kind of array that does not have got array.-methods like above.
elements = Array.prototype.slice.call(elements); // returns real array
于 2013-06-08T10:07:38.983 回答