I don't really understand prototypes, so it might be my fault, but theorically if I add a variable to a prototype, I will be able to change it in its instances, right?
Here the code:
<head>
<script>
CSSStyleDeclaration.prototype["foo"] = "something";
</script>
</head>
<body>
<div style="foo:'maybe'" id ="myId"></div>
<script>
var el = document.getElementById("myId");
console.log(el.style.foo);
</script>
</body>
The console returns "something", why?
That's because foo
is not a standard property, so
cssText
, the declaration is ignored getPropertyValue
would not have been able to retrieve the value. However, on browsers that support CSS variables , you could use them:
function getFoo(el) { return getComputedStyle(el).getPropertyValue('--foo'); } snippet.log("body: " + getFoo(document.body)); snippet.log("#myId: " + getFoo(document.getElementById('myId')));
* { --foo: 'something'; }
<div style="--foo: 'maybe'" id="myId"></div> <!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --><script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.