JavaScript中DOM树的Node接口的一些简单应用

1. 用类名查找元素

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN
" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
">

<html xmlns="http://www.w3.org/1999/xhtml
" >

<head>

    <title>Untitled Page</title>

    <script type="text/javascript
">

    //本页方法:用类名选择元素


        function
 getElements(cla) {

            var
 elements = document
.getElementsByTagName("*
");

            var
 elems = [];

            for
 (var
 i = 0; i < elements.length
; i++) {

                if
 (elements[i].className && has(elements[i].className,cla)) {

                    elems.push(elements[i]);

                }

            }

            return
 elems;

        }

        function
 has(ele, name
) {

            var
 clla = [];

            if
 (ele) clla = ele.split(' ');

            for
 (var
 i = 0; i < clla.length
; i++)

                if
 (clla[i] == name
) {return
 true
 };

            return
 false
;

        }

    </script>

    <style type="text/css
">

    .h

    {background-color:Aqua;

    }

    </style>

</head>

<body>

<i id="iiii
" class
="h
">Just test.</i><br />

<span id="ssss
" class
="h
">hehe</span>

<script type="text/javascript
">

    var
 e = getElements("h
");

    var
 nn = ""

    for
 (var
 i = 0; i < e.length
; i++) {

            nn +="id: 
" + e[i].id + "/n
";

        nn += "------line------/n
";

    }

    alert
(nn);

</script>

</body>

</html>

 

<script>// This function takes a Node n, replaces it in the tree with an Element node// that represents an HTML <b> tag, and then makes the original node the// child of the new<b> element.function embolden(n) { if (typeof n == "string") n = document.getElementById(n); // Lookup node var b = document.createElement("b"); // Create a new <b> element var parent = n.parentNode; // Get the parent of the node parent.replaceChild(b, n); // Replace the node with the<b> tag b.appendChild(n); // Make the node a child of the<b> element}</script><!-- A couple of sample paragraphs --><pid="p1">This<i>is</i> paragraph #1.</p><pid="p2">This<i>is</i> paragraph #2.</p><!-- A button that invokes the embolden() function on the element named p1 --><buttonοnclick="embolden('p1');">Embolden</button><script>// This function takes a Node n, replaces it in the tree with an Element node// that represents an HTML<b> tag, and then makes the original node the// child of the new<b> element.function embolden(n) { if (typeof n == "string") n = document.getElementById(n); // Lookup node var b = document.createElement("b"); // Create a new <b> element var parent = n.parentNode; // Get the parent of the node parent.replaceChild(b, n); // Replace the node with the<b> tag b.appendChild(n); // Make the node a child of the<b> element}</script><!-- A couple of sample paragraphs --><pid="p1">This<i>is</i> paragraph #1.</p><pid="p2">This<i>is</i> paragraph #2.</p><!-- A button that invokes the embolden() function on the element named p1 --><buttonοnclick="embolden('p1');">Embolden</button><script>// This function takes a Node n, replaces it in the tree with an Element node// that represents an HTML<b> tag, and then makes the original node the// child of the new<b> element.function embolden(n) { if (typeof n == "string") n = document.getElementById(n); // Lookup node var b = document.createElement("b"); // Create a new <b> element var parent = n.parentNode; // Get the parent of the node parent.replaceChild(b, n); // Replace the node with the<b> tag b.appendChild(n); // Make the node a child of the<b> element}</script><!-- A couple of sample paragraphs --><pid="p1">This<i>is</i> paragraph #1.</p><pid="p2">This<i>is</i> paragraph #2.</p><!-- A button that invokes the embolden() function on the element named p1 --><buttonοnclick="embolden('p1');">Embolden</button><script>// This function takes a Node n, replaces it in the tree with an Element node// that represents an HTML<b> tag, and then makes the original node the// child of the new<b> element.function embolden(n) { if (typeof n == "string") n = document.getElementById(n); // Lookup node var b = document.createElement("b"); // Create a new <b> element var parent = n.parentNode; // Get the parent of the node parent.replaceChild(b, n); // Replace the node with the<b> tag b.appendChild(n); // Make the node a child of the<b> element}</script><!-- A couple of sample paragraphs --><pid="p1">This<i>is</i> paragraph #1.</p><pid="p2">This<i>is</i> paragraph #2.</p><!-- A button that invokes the embolden() function on the element named p1 --><buttonοnclick="embolden('p1');">Embolden</button><script>// This function takes a Node n, replaces it in the tree with an Element node// that represents an HTML<b> tag, and then makes the original node the// child of the new<b> element.function embolden(n) { if (typeof n == "string") n = document.getElementById(n); // Lookup node var b = document.createElement("b"); // Create a new <b> element var parent = n.parentNode; // Get the parent of the node parent.replaceChild(b, n); // Replace the node with the<b> tag b.appendChild(n); // Make the node a child of the<b> element}</script><!-- A couple of sample paragraphs --><pid="p1">This<i>is</i> paragraph #1.</p><pid="p2">This<i>is</i> paragraph #2.</p><!-- A button that invokes the embolden() function on the element named p1 --><buttonοnclick="embolden('p1');">Embolden</button><script>// This function takes a Node n, replaces it in the tree with an Element node// that represents an HTML<b> tag, and then makes the original node the// child of the new<b> element.function embolden(n) { if (typeof n == "string") n = document.getElementById(n); // Lookup node var b = document.createElement("b"); // Create a new <b> element var parent = n.parentNode; // Get the parent of the node parent.replaceChild(b, n); // Replace the node with the<b> tag b.appendChild(n); // Make the node a child of the<b> element}</script><!-- A couple of sample paragraphs --><pid="p1">This<i>is</i> paragraph #1.</p><pid="p2">This<i>is</i> paragraph #2.</p><!-- A button that invokes the embolden() function on the element named p1 --><buttonοnclick="embolden('p1');">Embolden</button><script>// This function takes a Node n, replaces it in the tree with an Element node// that represents an HTML<b> tag, and then makes the original node the// child of the new<b> element.function embolden(n) { if (typeof n == "string") n = document.getElementById(n); // Lookup node var b = document.createElement("b"); // Create a new <b> element var parent = n.parentNode; // Get the parent of the node parent.replaceChild(b, n); // Replace the node with the<b> tag b.appendChild(n); // Make the node a child of the<b> element}</script><!-- A couple of sample paragraphs --><pid="p1">This<i>is</i> paragraph #1.</p><pid="p2">This<i>is</i> paragraph #2.</p><!-- A button that invokes the embolden() function on the element named p1 --><buttonοnclick="embolden('p1');">Embolden</button><script>// This function takes a Node n, replaces it in the tree with an Element node// that represents an HTML<b> tag, and then makes the original node the// child of the new<b> element.function embolden(n) { if (typeof n == "string") n = document.getElementById(n); // Lookup node var b = document.createElement("b"); // Create a new <b> element var parent = n.parentNode; // Get the parent of the node parent.replaceChild(b, n); // Replace the node with the<b> tag b.appendChild(n); // Make the node a child of the<b> element}</script><!-- A couple of sample paragraphs --><pid="p1">This<i>is</i> paragraph #1.</p><pid="p2">This<i>is</i> paragraph #2.</p><!-- A button that invokes the embolden() function on the element named p1 --><buttonοnclick="embolden('p1');">Embolden</button>



<
script
>

// This function takes a Node n, replaces it in the tree with an Element node
// that represents an HTML <
b
>
 tag, and then makes the original node the
// child of the new <
b
>
 element.
function embolden(n) {
    if (typeof n == "string") n = document.getElementById(n); // Lookup node
    var b = document.createElement("b"); // Create a new <
b
>
 element
    var parent = n.parentNode;           // Get the parent of the node
    parent.replaceChild(b, n);           // Replace the node with the <
b
>
 tag
    b.appendChild(n);                    // Make the node a child of the <
b
>
 element
}
</
script
>


<!-- A couple of sample paragraphs -->

<
p
 id
="p1"
>
This <
i
>
is</
i
>
 paragraph #1.</
p
>

<
p
 id
="p2"
>
This <
i
>
is</
i
>
 paragraph #2.</
p
>

<!-- A button that invokes the embolden() function on the element named p1 -->

<
button
 onclick
="embolden('p1');"
>
Embolden</
button
>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值