Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.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/2/jquery/68.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_Jquery_Html_Css - Fatal编程技术网

使用JavaScript创建样式

使用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

我正在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 (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我在寻找类似的答案