English 中文(简体)
如何从身体上而不是从头脑中挑选所有物品?
原标题:How to select all div from body and not from the header?

我使用这一javascript代码,以便选择所有DIVs并改变其肤色。 我不想在头脑中改变多民族的肤色。 这项任务的目的是学习超文本5、多功能、 Java字和内容。 通过TagName功能:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>test</title>
    <div>divv in header</div>
</head>
<body onload="Onload()">
    <div>bla</div>
    <div id="Div1">bla</div>
    <div id="Div2">bla
        <div id="Div4">div in div</div>
    </div>
    <div id="Div3" class="diiiivvv">bla</div>
</body>
</html>

<script type="text/javascript">
    function Onload() {
        var h = document.head;
        var dh = h.getElementsByTagName( div );
        if (dh.length != 0) {
            dh[0].style.backgroundColor =  red ; //fail
        }            

        var d = document.getElementsByTagName( div );

        for (var i = 0; i < d.length; i++) {
            d[i].style.backgroundColor =  blue ;
        };
    }
</script>
问题回答

问题何在? 你们才有权选择主人:

    var h = document.head;
    var dh = h.getElementsByTagName( div );
    // or short:
    var dh = document.head.getElementsByTagName( div );

因此,你在身体要素方面不使用同一要素:

    var db = document.body.getElementsByTagName( div );

https://developer.mozilla.org/en/DOM/element.getElementsByTagName”rel=“nofollow>getElementsByTagName( 这种方法可以适用于任何主角。

您可能认为,最好使用特别安全局的甄选者和适当的风格规则来查阅所需内容。 例如:

<script type="text/javascript">

function applyRule(selectorText, value) {

  // Get the style sheets - note sytleSheets is a live HTMLCollection
  var sheet, sheets = document.styleSheets;

  // Add a style sheet if there isn t one in the document
  if (!sheets.length) {
    sheet = document.createElement( style );
    sheet.type =  text/css ;
    document.getElementsByTagName( head )[0].appendChild(sheet);
  }

  // Get the last style sheet
  sheet = sheets[sheets.length - 1];

  // Add the rule - W3C model
  if (sheet.insertRule) {
    sheet.insertRule(selectorText +   {  + value +  } , sheet.cssRules.length);

  // IE model
  } else if (sheet.addRule) {
    sheet.addRule(selectorText, value, sheet.rules.length);
  }  
}
</script>

<div>here is a div
<button onclick="applyRule( div , background-color: red )">Change div background colour</button>
</div>




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签