>  기사  >  웹 프론트엔드  >  javascript는 xml을 읽고 xml 구현 code_javascript 기술을 작성합니다.

javascript는 xml을 읽고 xml 구현 code_javascript 기술을 작성합니다.

WBOY
WBOY원래의
2016-05-16 18:50:201189검색

데이터 추가:

데이터 표시:

ClassModel.js 소스 코드::

코드 복사 코드는 다음과 같습니다.

ClassModel =
{
create : function()
{
return function()
{
this.construct.apply(this, 인수)
}
}
Extend = function(desc, src)
{
for(var c in src)
> Return desc
}
Object.prototype.extend; = function(src)
{
return Extend.apply(this, [this, src]) 🎜>

addData.js 소스 코드::




코드 복사
코드는 다음과 같습니다.{
구성: 함수( config)
{
this.id = config.id;
this.name = config.name;
this.author = config.author
this.price = config.price;
this.publisher = config.publisher;
this.count = config.count;
this.insertData()
},
insertData: function()
{

var book = doc.createElement("book");

book.setAttribute("id", this.id)
var name = doc.createElement("name");
var nameValue = doc.createTextNode(this.name);
name.appendChild(nameValue);
book.appendChild(name)
varauthor = doc.createElement("author");
varauthorValue = doc.createTextNode(this.author);
author.appendChild(authorValue);
book.appendChild(author)
var price = doc.createElement("price");
var priceValue = doc.createTextNode(this.price);
price.appendChild(priceValue);
book.appendChild(price);

var count = doc.createElement("count ");
var countValue = doc.createTextNode(this.count);
count.appendChild(countValue);
book.appendChild(count);
var 게시자 = doc.createElement("publisher ");
varPublisherValue = doc.createTextNode(this.publisher);
publisher.appendChild(publisherValue);
book.appendChild(publisher);



if(doc.documentElement == null)
{

books = doc.createElement("books")
books.appendChild(book)
doc.appendChild(books);
}
else
{
books = doc.documentElement;
books.appendChild(book)

}
doc.save("books.xml" );
}
}



window.js 소스 코드::




코드 복사
코드는 다음과 같습니다.

var windows = ClassModel.create();
windows.prototype =
{
construct : function(jsonObject)
{
this.title = jsonObject.title;
this.width = jsonObject.width;
this.height = jsonObject.height;
this.titleColor = jsonObject.titleColor;
this.BackgroundColor = jsonObject.BackgroundColor;
this.LwHeight = (document.body.clientHeight - this.width) / 2; //让div재屏幕的中间
this.LwWidth = (document.body.clientWidth - this.height) / 2; //div재중
this.content = jsonObject.content;
var loginWindow = this.createLoginBody();
var title = this.createLoginTitle();
loginWindow.appendChild(제목);
var cont = this.createContent();
loginWindow.appendChild(계속);
document.body.appendChild(loginWindow);
},
createLoginBody: function() //创建登陆框, 即整个框
{
var loginWindow = document.createElement("div");
loginWindow.id = "대화상자";
with(loginWindow.style)
{
border = "1px 단색 흰색";
위치 = "절대";
너비 = this.width "px";
높이 = this.height "px";
top = this.LwHeight "px";
왼쪽 = this.LwWidth "px";
배경색 = this.BackgroundColor;
}
loginWindow를 반환합니다.
},
createLoginTitle:function() //创建 标题 即效果图적黑color标题
{
var title = document.createElement("div");
var table = document.createElement("table");
var tbody = document.createElement("tbody");
var tr = document.createElement("tr");
var td_1 = document.createElement("td");
var td_2 = document.createElement("td");
var close = document.createElement("a");
close.onclick = function()
{
document.body.removeChild(title.parentNode);
}
close.innerHTML = "X";
td_1.innerHTML = this.title;
with(title.style)
{
width = "100%";
높이 = this.height / 10 "px";
배경색상 = this.titleColor;
}
with(table.style)
{
color = "white";
fontSize = "12pt";
너비 = "100%";
배경색상 = this.titleColor;
색상 = "빨간색";
}
td_2.style.textAlign = "오른쪽";
td_2.appendChild(닫기);
tr.appendChild(td_1);
tr.appendChild(td_2);
tbody.appendChild(tr);
table.appendChild(tbody);
title.appendChild(테이블);
반품 제목;
},
createContent : function()
{
var div = document.createElement("div");
if(typeof(this.content) == '문자열')
{
div.innerHTML = this.content;
}else
{
div.appendChild(this.content);
}
with(div.style)
{
paddingLeft = "80px";
paddingTop = "50px";
float = "왼쪽";
너비 = "100%";
}
div 반환;
}
}

book_infor.js源码::
复主代码 代码아래:

var doc = new ActiveXObject("Msxml2.DOMDocument.3.0");
doc.load("books.xml");
var query = ClassModel.create();
var v = 0;
query.prototype =
{
    construct : function()
    {
        this.bookInfor();
    },
    bookInfor : function()
    {
        var div = document.createElement("div");
        var root = doc.documentElement;
        if(root == null)
        {
            div.innerHTML = "데이터 없음";
            document.body.appendChild(div);
        }else
        {

        
        with(div.style)
        {
           marginLeft = "200px";
            오버플로 = "자동";
            테두리 = "0px 단색 흰색";
            너비 = "605px";

        }
        var table = document.createElement("table");
        table.cellSpacing = "0";
        with(table.style)
        {    
            fontSize = "12pt";
            색상 = "흰색";
            테두리 = "0px";
            너비 = "600px";

        }
        var tbody = document.createElement("tbody");
        var trHead = document.createElement("tr");
        with(trHead.style)
        {
            height = "20px";
             backgroundColor = "투명";

        }
        var tname = document.createElement("td");
        var tauthor = document.createElement("td");
        var tprice = document.createElement("td");
        var tCount = document.createElement("td");
        var tpublisher = document.createElement("td");

        tname.innerHTML = "이름";
        tauthor.innerHTML = "작자";
        tprice.innerHTML = "가격";
        tCount.innerHTML = "库存";
        tpublisher.innerHTML = "출판사";
        tname.style.borderBottom = "1px 단색";
        tauthor.style.borderBottom = "1px 단색";
        tprice.style.borderBottom = "1px 단색";
        tCount.style.borderBottom = "1px 단색";
        tpublisher.style.borderBottom = "1px solid";

        tname.style.width = "20%";
        tauthor.style.width = "20%";
        tprice.style.width = "20%";
        tCount.style.width = "20%";
        tpublisher.style.width = "20%";
        trHead.appendChild(tname);
        trHead.appendChild(tauthor);
        trHead.appendChild(tprice);
        trHead.appendChild(tCount);
        trHead.appendChild(tpublisher);

        tbody.appendChild(trHead);

    

        for(var c = 0; c < root.getElementsByTagName("book").length; c )
        {
            varroots = root.getElementsByTagName(" 책")[c];
            var id =roots.getAttribute("id");
            var name =roots.getElementsByTagName("name")[0].childNodes[0].nodeValue;
            var 작성자 =roots.getElementsByTagName("author")[0].childNodes[0].nodeValue;
            var 가격 = 뿌리.getElementsByTagName("price")[0].childNodes[0].nodeValue;
            var count =roots.getElementsByTagName("count")[0].childNodes[0].nodeValue;
            var 게시자 = 뿌리.getElementsByTagName("publisher")[0].childNodes[0].nodeValue;
            var tr = document.createElement("tr");

            with(tr.style)
            {
               backgroundColor = "투명";
            }

            var tdName = document.createElement("td");
            var tdAuthor = document.createElement("td");
            var tdPrice = document.createElement("td");
            var tdCount = document.createElement("td");
            var tdPublisher = document.createElement("td");

            
            tdName.innerHTML = 이름;
            tdAuthor.innerHTML = 작성자;
            tdPrice.innerHTML = 가격;
            tdCount.innerHTML = 개수;
            tdPublisher.innerHTML = 게시자;

    
            tdName.id = "tdName" c;
            tdAuthor.id = "tdAuthor" c;
            tdPrice.id = "tdPrice" c;
            tdCount.id = "tdCount" c;
            tdPublisher.id = "tdPublisher" c;
            tr.appendChild(tdName);
            tr.appendChild(tdAuthor);
            tr.appendChild(tdPrice);
            tr.appendChild(tdCount);
            tr.appendChild(tdPublisher);
            tbody.appendChild(tr);

            tdName.onmouseover = function(){
               document.body.style.cursor= "포인터";
                document.getElementById(this.id).style.BackgroundColor = "어두운";
            }
            tdName.onmouseout = function(){
               document.body.style.cursor= "";
                document.getElementById(this.id).style.BackgroundColor = "";
            }
            tdAuthor.onmouseover = function(){
               document.body.style.cursor= "포인터";
                document.getElementById(this.id).style.BackgroundColor = "어두운";
            }
            tdAuthor.onmouseout = function(){
               document.body.style.cursor= "";
                document.getElementById(this.id).style.BackgroundColor = "";
            }
            tdPrice.onmouseover = function(){
               document.body.style.cursor= "포인터";
                document.getElementById(this.id).style.BackgroundColor = "어두운";
            }
            tdPrice.onmouseout = function(){
                document.body.style.cursor= "";
                document.getElementById(this.id).style.BackgroundColor = "";
            }
            tdCount.onmouseover = function(){
               document.body.style.cursor= "포인터";
                document.getElementById(this.id).style.BackgroundColor = "어두운";
            }
            tdCount.onmouseout = function(){
               document.body.style.cursor= "";
                document.getElementById(this.id).style.BackgroundColor = "";
            }
            tdPublisher.onmouseover = function(){
               document.body.style.cursor= "포인터";
                document.getElementById(this.id).style.BackgroundColor = "어두운";
            }
            tdPublisher.onmouseout = function(){
               document.body.style.cursor= "";
                document.getElementById(this.id).style.BackgroundColor = "";
            }
        }

        table.appendChild(tbody);
        div.appendChild(테이블);

        document.body.appendChild(div);

        
    }    
}    
}    

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.