我设法使用 -
显示XMLwxml = window.open("my_template.xml", "my_xml" );
我设法使用 -
更改DOMxDoc = wxml.document;
xNodes = xExDoc.getElementsByTagName("myNodeName");
xValue = xNodes[i].getElementsByTagName("value")[0];
xValue.firstChild.nodeValue = nodeNewVal;
但我无法在屏幕上看到新的DOM值。
如何强制“按DOM刷新屏幕”?
注意:reload()没有帮助,因为它加载了原始页面,我希望看到包含DOM更改的页面。
编辑 - 我使用的代码:
XML文件(my_template.xml):
<myXmlRoot>
<device>
<input><name>"name 1"</name><value>{replaceMe!}</value></input>
<input><name>"name 2"</name><value>{replaceMe!}</value></input>
</device>
<device>
<input><name>"name 1"</name><value>{replaceMe!}</value></input>
<input><name>"name 2"</name><value>{replaceMe!}</value></input>
</device>
<device>
<input><name>"name 1"</name><value>{replaceMe!}</value></input>
<input><name>"name 2"</name><value>{replaceMe!}</value></input>
</device>
</myXmlRoot>
HTML文件:
<html>
<head>
<title>Open XML in External Window</title>
</head>
<body>
<button onClick="fShowXmlInExternalWin()">Show XML </button> (does not show the updated version on the screen)
<script type="text/javascript" >
var wxml;
var xDoc;
var xDevices, xInputs;
var xDevice, xInput;
function fSetXmlAInput(iDevice, iNode, nodeNewVal) {
xInput = xInputs[iNode];
xValue = xInput.getElementsByTagName("value")[0];
// change node value:
// console.log("nodeVal: " + xValue.firstChild.nodeValue);
xValue.firstChild.nodeValue = nodeNewVal;
// console.log("newVal: " + xValue.firstChild.nodeValue);
}
function fSetXmlDevice(iDevice) {
xDevice = xDevices[iDevice];
xInputs = xDevice.getElementsByTagName("input");
fSetXmlAInput(iDevice, 0, "22");
fSetXmlAInput(iDevice, 1, "33");
}
function fShowXmlInExternalWin() {
wxml = window.open("my_template.xml", "my_xml" );
xDoc = wxml.document;
xDevices = xDoc.getElementsByTagName("device");
fSetXmlDevice(1);
return false;
}
</script>
</body>
</html>
答案 0 :(得分:1)
在第一个视图中,您有以下错误:
Timestamp: 6/5/2013 2:32:11 μμ
Error: ReferenceError: xExDoc is not defined
我没有看到代码中某处定义了xExDoc ......我只看到xDoc。
此外,您的i变量未定义导致其他错误。 此外,您应该使用firebug逐步调试代码或至少添加
alert(xNodes.length);
以检查找到的标签数量。
我找到了两种可能的选择:
选项1使XML浏览器格式保持不变,而选项2使浏览器不再查看XML而不是xml内容,格式化将丢失。
代码如下:
<html>
<head>
<title>Open XML in External Window</title>
</head>
<body>
<button onClick="fShowXmlInExternalWin()">Show XML </button> (shows updated on the screen but loses XML formatting)<br/>
<button onClick="alternativeLoadXML()">Alternative Show XML </button> (shows updated XML, as XML is loaded - changed, and XML is rendered)<br/>
<button onClick="alternativeLoadXML2()">Alternative Show XML 2 </button> (open window with original XML, change XML model, reload window)<br/>
<script type="text/javascript" >
var wxml;
var xDoc;
var xDevices, xInputs;
var xDevice, xInput;
function fSetXmlAInput(iDevice, iNode, nodeNewVal) {
xInput = xInputs[iNode];
xValue = xInput.getElementsByTagName("value")[0];
// change node value:
// console.log("nodeVal: " + xValue.firstChild.nodeValue);
xValue.firstChild.nodeValue = nodeNewVal;
// console.log("newVal: " + xValue.firstChild.nodeValue);
}
function fSetXmlDevice(iDevice) {
xDevice = xDevices[iDevice];
xInputs = xDevice.getElementsByTagName("input");
fSetXmlAInput(iDevice, 0, "22");
fSetXmlAInput(iDevice, 1, "33");
}
function fShowXmlInExternalWin() {
wxml = window.open("my_template.xml", "my_xml" );
//Delay 500ms for window to load first
window.setTimeout("triggerWindow()",500);
return false;
}
//Further options Below
//First option, trigger refresh with append and remove - loses formatting
function triggerWindow()
{
xDoc = wxml.document;
xDevices = xDoc.getElementsByTagName("device");
fSetXmlDevice(1);
//Add and remove element to trigger referesh
var p = document.createElement("test");
xDoc.firstChild.appendChild(p);
xDoc.firstChild.removeChild(p);
}
function alternativeLoadXML()
{
// load xml file
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else { // IE 5/6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", "my_template.xml", false);
xhttp.send();
xDoc = xhttp.responseXML;
xDevices = xDoc.getElementsByTagName("device");
fSetXmlDevice(1);
var xmlText = new XMLSerializer().serializeToString(xDoc);
window.open('data:text/xml,' + xmlText);
}
//Second option, open window, change XML, reload window with custom xml address
function triggerWindow2()
{
xDoc = wxml.document;
xDevices = xDoc.getElementsByTagName("device");
fSetXmlDevice(1);
var xmlText = new XMLSerializer().serializeToString(xDoc);
window.open('data:text/xml,' + xmlText, "my_xml");
}
function alternativeLoadXML2()
{
wxml = window.open("my_template.xml", "my_xml" );
//Delay 500ms for window to load first
window.setTimeout("triggerWindow2()",500);
}
</script>
</body>
</html>
在新窗口中使用document.open和document.write在IE中工作以输出正确的XML但XML呈现已关闭 - 似乎将内容呈现为HTML ...
function alternativeLoadXML3() {
// load xml file
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else { // IE 5/6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", "my_template.xml", false);
xhttp.send();
xDoc = xhttp.responseXML;
xDevices = xDoc.getElementsByTagName("device");
fSetXmlDevice(1);
var xmlText = serializeXmlNode(xDoc);
var newWindow = window.open("my_template.xml", "Test", "width=300,height=300,scrollbars=1,resizable=1");
newWindow.document.open();
newWindow.document.write(xmlText);
newWindow.document.close()
};