使用JavaScript创建样式
我正在div数组上应用样式值,它工作正常。我可以在下面的代码中找到带有类的div,但我也必须选择带有类的部分和文章使用JavaScript创建样式,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我正在div数组上应用样式值,它工作正常。我可以在下面的代码中找到带有类的div,但我也必须选择带有类的部分和文章 var selectors = ["header-wrap", "top-page", "footer","sidepanels"]; var arrayLength = selectors.length; for (var i = 0; i < arrayLength; i++) { var parentDoc = window; while (par
var selectors = ["header-wrap", "top-page", "footer","sidepanels"];
var arrayLength = selectors.length;
for (var i = 0; i < arrayLength; i++) {
var parentDoc = window;
while (parentDoc !== parentDoc.parent) {
parentDoc = parentDoc.parent;
}
parentDoc = parentDoc.document;
//getting div content
var divContent = parentDoc.getElementsByClassName(selectors[i])[0];
if (divContent) {
divContent.style.position = 'relative';
divContent.style.zIndex = '1';
}
上面的代码运行良好。在jquery中有没有更好的方法来做同样的事情?是的,在jquery中可以很容易地做到这一点 假设jQuery在根窗口中被引用,您可以执行以下操作:
// you don't need to loop through the parents to get the root window, you can use window.top
window.top.$('.header-wrap', '.top-page', '.footer', '.sidepanels') // pass the classes to $
.css({ position: 'relative', zIndex: 1 }); // changes the CSS of all the found elements
$(window.top.document) // get the most parent document
.find('.header-wrap', '.top-page', '.footer', '.sidepanels') // pass the classes to jQuery
.css({ position: 'relative', zIndex: 1 }); // changes the CSS of all the found elements
如果iframe中引用了jQuery,则可以执行以下操作:
// you don't need to loop through the parents to get the root window, you can use window.top
window.top.$('.header-wrap', '.top-page', '.footer', '.sidepanels') // pass the classes to $
.css({ position: 'relative', zIndex: 1 }); // changes the CSS of all the found elements
$(window.top.document) // get the most parent document
.find('.header-wrap', '.top-page', '.footer', '.sidepanels') // pass the classes to jQuery
.css({ position: 'relative', zIndex: 1 }); // changes the CSS of all the found elements
这是原因吗?你在尝试什么版本的IE?谢谢Stepan我在寻找类似的答案