gpt4 book ai didi

javascript - 在javascript中设置css属性

转载 作者:太空宇宙 更新时间:2023-11-04 04:36:33 25 4
gpt4 key购买 nike

这是我的函数集 css 属性,就像 jquery

that.getNode = function() {
return element; // element is HTML node
};


that.css = function(obj) {
var node = that.getNode(),
attr;
if (node && node.style) {
for (attr in obj) {
if (obj.hasOwnProperty(attr) && node.style.hasOwnProperty(attr)) {
node.style[attr] = obj[attr];
}
}
}
return that;
};

然后我调用,它在Chrome上运行正常,但在IE9或firefox上无法运行

that.css({
border: "1px solid black"
});

最佳答案

似乎以下内容在某些浏览器中返回 false:

node.style.hasOwnProperty(attr)

在这些情况下,属性是从 prototype 继承的,而不是由 Object 拥有(node.style) 本身。

如果您想测试该属性是否存在,无论是拥有的还是继承的,您可以尝试 in operator :

attr in node.style

在上下文中:

that.css = function(obj) {
var node = that.getNode(),
attr;
if (node && node.style) {
for (attr in obj) {
if (obj.hasOwnProperty(attr) && attr in node.style) {
node.style[attr] = obj[attr];
}
}
}
return that;
};

关于javascript - 在javascript中设置css属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16271083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com