Javascript:使用appendChild()创建div不会';好像不行
我有一个AngularJS应用程序,我正在使用。我已经注入了一个第三方JS代码段,该代码段应该创建一个div并将其附加到index.html文件的Javascript:使用appendChild()创建div不会';好像不行,javascript,Javascript,我有一个AngularJS应用程序,我正在使用。我已经注入了一个第三方JS代码段,该代码段应该创建一个div并将其附加到index.html文件的,但它似乎不起作用。未在我的应用程序中的上创建div JS代码如下所示: window.onload = function(){ console.log('Initiated cursive.js'); /* * Create topbar */ var topBar = document.createElement('div');
,但它似乎不起作用。未在我的应用程序中的上创建div
JS代码如下所示:
window.onload = function(){
console.log('Initiated cursive.js');
/*
* Create topbar
*/
var topBar = document.createElement('div');
console.log(topBar);
topBar.style.width = '100%';
topBar.style.height = '20px';
topBar.style.position = 'absolute';
topBar.style.background = '#4C5264';
topBar.style.top = '0';
topBar.style.left = '0';
/*
* Append topbar to body
*/
document.getElementsByTagName('body')[0].appendChild(topBar);
};
index.html文件:
<!doctype html>
<html lang="en">
<head>
...
</head>
<body ui-view>
...
</body>
</html>
...
...
我做错了什么?您的代码是正确的,并且工作正常。由于其他控件的原因,您可能看不到topBar div。要确保topBar位于DOM中,可以将z-index设置为9999,在这种情况下,topBar div应该可见
topBar.style.zIndex = 9999;
document.body.appendChild(顶栏)
?如果我错了,请纠正我,但我假设在您的示例中,body
元素表现为角度视图
容器,因此附加到它的所有内容都将被Angular覆盖。您到底想实现什么。。你能告诉我吗?@Shreejibawa我正试图通过第三方js脚本向我的应用程序添加一个topbar。与intercom.io och Olark(聊天小部件)类似。我认为你关于角度覆盖内容的说法是正确的。@Teemu我已经编辑了这个问题。谢谢!错过了那部分。