prototype 学习
这段时间不是很忙的,终于有机会学习一下javascript,这块一直是我的弱项,得找个机会补习一下啊,
通过两天的学习,有了个大体的认识,
学习过程中可以参考 http://591wap.cn/works/prototype/index.html
对大家学习应该有帮助的,
<input type="hidden" id="hidxml" runat="server" /> //xml 文件的地址
<input type="button" value="GetHtml" onclick="GetXml()" />
<div id="placeholder"></div>
<textarea id="result" cols='60' rows='10' ></textarea>
<?xml version="1.0" encoding="utf-8"?>
<Books>
<Book>
<Name>HTML Book</Name>
<Author>Lee</Author>
</Book>
<Book>
<Name>XML Book</Name>
<Author>liu</Author>
</Book>
</Books>
function showBooks(originalRequest) //本来打算把返回的xml流转化为html 的table 的格式
{
var results = originalRequest.responseXML;
var table = document.createElement("table");
var books = results.getElementsByTagName("Book");
alert(books.length); // ---总是0
for(var i = 0; i < books.length; i++) {
book = books[i];
var name = book.getElementsByTagName("Name")[0].firstChild.nodeValue;
var author = book.getElementsByTagName("Author")[0].firstChild.nodeValue;
var row = document.createElement("tr");
var cellname=document.createElement("td");
var cellauthor=document.createElement("td");
cellname.innerText = name;
cellauthor.innerText=author;
row.appendChild(cellname);
row.appendChild(cellauthor);
table.appendChild(row);
}
var con=$("placeholder");
con.innerHTML = table.outerHTML;
}
function GetXml()
{
var url = './GetXml.ashx';
var pars = 'file='+$F("hidxml");
var myAjax = new Ajax.Request(
url,
{method: 'get', parameters: pars, onComplete: showBooks}
);
// var myAjax = new Ajax.Updater({success: 'placeholder'},url,{method: 'get',parameters: pars,onFailure: reportError});
}
function reportError()
{
alert('ERROR');
}
function Suecss()
{
alert(" over");
}
./GetXml.ashx 是为了生成xml文件的,具体如下
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class GetXml : IHttpHandler {
public void ProcessRequest(HttpContext context) {
string fileName = context.Request.QueryString["file"];
context.Response.ContentType = "text/plain";
context.Response.Write(CreateXmlDocument(fileName));
}
public bool IsReusable {
get {
return false;
}
}
private string CreateXmlDocument(string file) {
string Result = string.Empty;
XmlTextWriter writer = new XmlTextWriter(file, System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.Indentation = 6;
writer.WriteStartDocument();
writer.WriteStartElement("Books");
GenerateClassXmlNode(writer);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
XmlDocument xdoc = new XmlDocument();
xdoc.Load(file);
return xdoc.OuterXml;
}
private void GenerateClassXmlNode(XmlTextWriter writer) {
writer.WriteStartElement("Book");
writer.WriteElementString("Name", "HTML Book");
writer.WriteElementString("Author", "Lee");
writer.WriteEndElement();
writer.WriteStartElement("Book");
writer.WriteElementString("Name", "XML Book");
writer.WriteElementString("Author", "liu");
writer.WriteEndElement();
}
那位高手能解答一下啊,谢谢拉