【问题标题】:XML interfering with javascript variablesXML 干扰 javascript 变量
【发布时间】:2014-09-02 04:08:24
【问题描述】:

这是我正在进行的项目的简化版本。

<!DOCTYPE html>
<html>
<body>

<script type="text/javascript">
    var bridge;

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET","MarketSpreadsheet.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML; 


    bridge = "xml data goes in here";

</script>


<svg height="300" width="300">
    <text id="thePopUp" x="150" y="150" font-size="30" fill="black" visibility="visible"></text>
</svg>

<script type="text/javascript">
thePopUp.textContent = bridge;
</script>

</body>
</html>

我正在尝试创建一个动态 svg 元素,但由于某种原因,将 xml 字符串直接解析为 thePopUp.textContent 不起作用。我的计划是将 xml 数据转换为 javascript 变量矩阵并使用它来分配文本内容。我的问题是 xml.http.send();行使我的变量分配无效,使输出“未定义”。我需要该行来访问我的 xml 文件,但该行阻止我将 xml 分配给一个变量(或任何东西给一个变量)。这是怎么回事?

【问题讨论】:

  • 也许你应该先问问自己为什么要同时支持 IE5 和使用嵌入式 SVG。那么,xmlhttp.send() 是否真的使下面的语句无效,或者xmlhttp.responseXML 是否真的是undefined 因为MarketSpreadsheet.xml 上的Content-Type 错误,或者请求没有成功。然后将 XML DOM 放在 SVG &lt;text&gt; 元素的 textContent 中是否有意义。

标签: javascript html xml svg


【解决方案1】:
SAMPLE XML 
<exam courseName="MS Word" methodTxt="WRITE THE CORRECT OPTION IN ANSWER FIELD" exam_no="1" blankFeedback="Please write your answer">
    <item>
        <question><![CDATA[What is new in MS Word 2010?What is new in MS Word 2010?What is new in MS Word 2010?What is new in MS Word 2010?What is new in MS Word 2010?What is new in MS Word 2010?What is new in MS Word 2010?tapash]]></question>
        <acceptable_option this_question_marks="2">
            <option marks="2"><![CDATA[Tabs]]></option>
            <option marks=".4"><![CDATA[Buttons]]></option>
            <option marks=".4"><![CDATA[Check boxes]]></option>
            <option marks=".4"><![CDATA[Dialog boxes]]></option>
        </acceptable_option>
    </item>
    <item>
        <question><![CDATA[The Ribbon is divided into]]></question>
        <acceptable_option this_question_marks="2">
            <option marks="2"><![CDATA[Tabs]]></option>
        </acceptable_option>
    </item>
</exam>

AND JAVASCRIPT IS 
if (window.XMLHttpRequest){
        xmlhttp=new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET","xml/fill_in_the_blank.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;


    myXMLDoc=xmlDoc.getElementsByTagName('item');
    totalQuestion = myXMLDoc.length;

$("#thePopUp").text(totalQuestion)

但你需要使用 JQUERY 和“WEB SERVER 或 WAMP 之类的东西”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多