Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.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/8/svg/2.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:在每个DOM元素中一次性插入大型CSS字符串_Javascript_Css_Dom_Walkthrough - Fatal编程技术网

javascript:在每个DOM元素中一次性插入大型CSS字符串

javascript:在每个DOM元素中一次性插入大型CSS字符串,javascript,css,dom,walkthrough,Javascript,Css,Dom,Walkthrough,这是一个由两部分组成的问题-首先,我需要获取父元素的子元素(或子元素等),然后我需要重置它的样式。因此,我希望做如下工作: var everything = parent.getEveryElementBelowThisOne(); for (i=0; i<everything.length; i++) everything[i].css = "font: 100%/100% Verdana,Arial,Helvetica,sans-serif; color: rgb(0, 0,

这是一个由两部分组成的问题-首先,我需要获取父元素的子元素(或子元素等),然后我需要重置它的样式。因此,我希望做如下工作:

var everything = parent.getEveryElementBelowThisOne();
for (i=0; i<everything.length; i++)
    everything[i].css = "font: 100%/100% Verdana,Arial,Helvetica,sans-serif; color: rgb(0, 0, o); margin: 0px; padding: 0px; border-collapse: collapse; border-width: 0px; border-spacing: 0px; text-align: left; outline: 0pt none; text-transform: none; vertical-align: middle; background-color: transparent; table-layout: auto; min-width: 0px; min-height: 0px;"

jQuery不是一个选项。

我会使用一个可读性和可维护性更高的对象,而不是字符串:

var new_css = {
    font: '100%/100% Verdana,Arial,Helvetica,sans-serif',
    color: 'rgb(0, 0, o)',
    margin: '0px',
    padding: '0px',
    borderCollapse: 'collapse'
    /* rest here ... */
}
然后使用助手函数,类似于:

function setStyle (element, style) {
    for (var n in style) {
        element[n] = style[n];
    }
}
并进入您的for循环:

for (i=0; i<everything.length; i++) setStyle(everything[i],new_css);

对于(i=0;i,我将使用可读性和可维护性更高的对象,而不是字符串:

var new_css = {
    font: '100%/100% Verdana,Arial,Helvetica,sans-serif',
    color: 'rgb(0, 0, o)',
    margin: '0px',
    padding: '0px',
    borderCollapse: 'collapse'
    /* rest here ... */
}
然后使用助手函数,类似于:

function setStyle (element, style) {
    for (var n in style) {
        element[n] = style[n];
    }
}
并进入您的for循环:

for (i=0; i<everything.length; i++) setStyle(everything[i],new_css);

for(i=0;i这有点离奇,当你谈到父元素时,我们假设你在某个时候会考虑它的子元素。但是当你说,
这个元素下面的每个元素都可能是相关元素后面的DOM元素。你的可能是其中之一

我假设您想要更改下一个元素的样式。 使用原始javascript,您可以以通用循环方式进行遍历,如下所示

nS = parent.nextElementSibling
while(nS){
  nS.style.width = '100%';
  // Change the desired style here
  // You can also further loop on nS's children using `nS.childNodes`,
  // if you want to change their styles too
  nS = nS.nextElementSibling;
}
正如您在原始javascript中看到的,更改样式的方式非常令人反感。 另一方面,jQuery提供了良好的DOM特性,包括简单的遍历,甚至是样式

就像jQuery中的一样

$(parent).nextAll().each(function(){
      $(this).css({'width': '100%', 'other': 'rules', 'as': 'one-dict'});
      // Change the desired style here
      // You can also further loop nS's children using `$(this).children()`, 
      // if you want to change their styles too
});

希望这能有所帮助。

这有点离奇,因为当你谈到父元素时,我们假设你在某个时候会考虑它的子元素。但是当你说,
这个元素下面的每个元素都可能是相关元素后面的DOM元素。你的可能是其中之一

我假设您想要更改下一个元素的样式。 使用原始javascript,您可以以通用循环方式进行遍历,如下所示

nS = parent.nextElementSibling
while(nS){
  nS.style.width = '100%';
  // Change the desired style here
  // You can also further loop on nS's children using `nS.childNodes`,
  // if you want to change their styles too
  nS = nS.nextElementSibling;
}
正如您在原始javascript中看到的,更改样式的方式非常令人反感。 另一方面,jQuery提供了良好的DOM特性,包括简单的遍历,甚至是样式

就像jQuery中的一样

$(parent).nextAll().each(function(){
      $(this).css({'width': '100%', 'other': 'rules', 'as': 'one-dict'});
      // Change the desired style here
      // You can also further loop nS's children using `$(this).children()`, 
      // if you want to change their styles too
});

希望这有帮助。

使用
myElement.style.cssText

var everything = parent.getEveryElementBelowThisOne();
for (i=0; i<everything.length; i++)
    everything[i].style.cssText = "font: 100%/100% Verdana,Arial,Helvetica,sans-serif; color: rgb(0, 0, o); margin: 0px; padding: 0px; border-collapse: collapse; border-width: 0px; border-spacing: 0px; text-align: left; outline: 0pt none; text-transform: none; vertical-align: middle; background-color: transparent; table-layout: auto; min-width: 0px; min-height: 0px;"

使用myElement.style.cssText

var everything = parent.getEveryElementBelowThisOne();
for (i=0; i<everything.length; i++)
    everything[i].style.cssText = "font: 100%/100% Verdana,Arial,Helvetica,sans-serif; color: rgb(0, 0, o); margin: 0px; padding: 0px; border-collapse: collapse; border-width: 0px; border-spacing: 0px; text-align: left; outline: 0pt none; text-transform: none; vertical-align: middle; background-color: transparent; table-layout: auto; min-width: 0px; min-height: 0px;"

谢谢,对于我的第二个问题,这是一个很好的建议。现在你知道一种遍历每个节点的dom树的方法吗?每个dom节点,包括
document
本身都有一个类似数组的子节点集合,称为
childNodes
。只需递归遍历它们。例如,请参阅:谢谢,这是一个cellent对我的第二个问题提出了建议。现在你知道一种遍历每个节点的dom树的方法了吗?每个dom节点,包括
document
本身都有一个类似数组的子节点集合,称为
childNodes
。只需递归遍历它们。例如,请参阅:和