我的代码是否有错误或与我的Firefox 12浏览器有关? [英] Is there an error in my code or does it have to do with my Firefox 12 browser?

查看:145
本文介绍了我的代码是否有错误或与我的Firefox 12浏览器有关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从教程中复制了以下代码,但仍然无法弄清楚我是否在某处出错,或者是否与浏览器支持有关。感谢提前向我指出发生了什么!

 < html> 
< head>
< script type =text / javascript>

函数loadXMLDoc(dname)
{
if(window.XMLHttpRequest)
{
xhttp = new XMLHttpRequest();
}
else
{
xttp = new ActiveXObject(Microsoft.XMLHTTP);
}

xhttp.open(GET,dname,false);
xhttp.send();
return xhttp.responseXML;
}

函数change(text)
{
var xmlDoc = loadXMLDoc(dom.xml);
var x = xmlDoc.getElementsByTagName(title)[0] .childNodes [0];

x.nodeValue = text;

var y = xmlDoc.getElementsByTagName(title);
for(i = 0; i< y.length; i ++)
{
document.write(y [i] .childNodes [0] .nodeValue +< br /> );
}
}

函数remove(node)
{
xmlDoc = loadXMLDoc(dom.xml);
var y = xmlDoc.getElementsByTagName(node)[0];

xmlDoc.documentElement.removeChild(y);
alert(元素+节点+已被删除!);
}

函数证明(u)
{
var x = xmlDoc.getElementsByTagName(u); (i = 0; i {
document.write(x [i] .childNodes [0] .nodeValue);

document.write(< br />);
}

< / script>
< / head>
< body>
< input type =buttonvalue =removeonclick =remove('book')/>
< input type =buttonvalue =证明onclick =证明('book')/>
< / body>

< / html>

------------更新-------- -----------------



这是一个可能有帮助的XML文件:

 < bookstore> 
< book category =烹饪>
< title lang =en>日常意大利语< / title>
< author> Giada< / author>
< year> 2005< / year>
< price> 30.00< / price>
< / book>
< book category =烹饪>
< title lang =en>书2< / title>
< author> Giada< / author>
< year> 2005< / year>
< price> 30.00< / price>
< / book>
< book category =烹饪>
< title lang =en>书3< / title>
< author> Giada< / author>
< year> 2005< / year>
< price> 30.00< / price>
< / book>
< / bookstore>


解决方案

我认为问题可能是因为 document.write


在页面完成加载$ b $之后运行的任何document.write语句b将创建一个新页面并覆盖当前
页面的所有内容。


还有一个 innerHTML div 或某些html元素以克服这个问题。



除此之外,我没有发现您的代码有任何问题


I copied the following code from a tutorial, but still couldn't figure out whether I made a mistake somewhere or whether it has to do with the browser support. Thanks in advance for pointing out to me on what happened!

<html>
<head>
<script type="text/javascript">

function loadXMLDoc(dname)
{   
    if(window.XMLHttpRequest)
    {
        xhttp = new XMLHttpRequest();
    }
    else
    {
        xttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xhttp.open("GET", dname, false);
    xhttp.send();
    return xhttp.responseXML;
}

function change(text)
{
    var xmlDoc = loadXMLDoc("dom.xml");
    var x = xmlDoc.getElementsByTagName("title")[0].childNodes[0];

    x.nodeValue = text;

    var y = xmlDoc.getElementsByTagName("title");
    for(i=0; i<y.length; i++) 
    {
        document.write(y[i].childNodes[0].nodeValue+"<br />");
    }
}

function remove(node)
{
    xmlDoc = loadXMLDoc("dom.xml");
    var y = xmlDoc.getElementsByTagName(node)[0];

    xmlDoc.documentElement.removeChild(y);
    alert("The element "+node+" has been removed!");
}

function prove(u)
{
    var x = xmlDoc.getElementsByTagName(u);
    for (i=0; i<x.length; i++)
    {
        document.write(x[i].childNodes[0].nodeValue);
        document.write("<br />");
}

</script>
</head>
<body>
    <input type="button" value="remove" onclick="remove('book')" />
    <input type="button" value="prove it" onclick="prove('book')" />
</body>

</html>

------------Update-------------------------

Here's an XML file that may help:

<bookstore>
    <book category="cooking">
        <title lang="en">Everyday Italian</title>
        <author>Giada</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book category="cooking">
        <title lang="en">Book 2</title>
        <author>Giada</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book category="cooking">
        <title lang="en">Book 3</title>
        <author>Giada</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
</bookstore>

解决方案

I think the problem might be because of document.write

Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page.

Also there is a } missing after the for statement of prove function

Try using innerHTML on a div or some html element to overcome this issue.

Other than that I don't find any issues with your code

这篇关于我的代码是否有错误或与我的Firefox 12浏览器有关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆