Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么样式表是';插入后的所有者节点是否为空?_Javascript_Css_Cssom - Fatal编程技术网

Javascript 为什么样式表是';插入后的所有者节点是否为空?

Javascript 为什么样式表是';插入后的所有者节点是否为空?,javascript,css,cssom,Javascript,Css,Cssom,有人能解释一下为什么样式表的所有者节点在我将其textContent设置为某个值后是null const style=document.createElement('style')) document.head.appendChild(样式) const sheet=style.sheet //通行证 assert(sheet.ownerNode!==null,“ownerNode为null”) sheet.ownerNode.textContent='div{color:red}' //失败

有人能解释一下为什么
样式表的
所有者节点
在我将其
textContent
设置为某个值后是
null

const style=document.createElement('style'))
document.head.appendChild(样式)
const sheet=style.sheet
//通行证
assert(sheet.ownerNode!==null,“ownerNode为null”)
sheet.ownerNode.textContent='div{color:red}'
//失败

assert(sheet.ownerNode!==null,“ownerNode为null”)
因为通过完全替换节点的文本,您已经创建了一个新的替换样式表对象:

const style=document.createElement('style'))
document.head.appendChild(样式)
const sheet=style.sheet
sheet.ownerNode.textContent='div{color:red}'

console.log(style.sheet==sheet);//有趣的问题!