是否可以将 domparser 元素更改为字符串?
Is it possible to change domparser element to string?
我有一些 HTML 字符串。使用 domparser 更新一些值,现在我需要返回 HTML 具有更新值的字符串格式... Bcoz document.write 只接受字符串。
检查示例补丁,
const domName = 'MOBILE_NO';
// Below dom was getting from api.
const dom = '<html><head><title>Merchant Checkout Page</title></head><body><center><h1>Please do not refresh this page...</h1></center><form method="post" name="paytm_form"><input type="hidden" name="MOBILE_NO" value="xxxxxxxx"></form></body></html>';
const parser = new DOMParser();
const parsedHtml = parser.parseFromString(dom, 'text/html');
parsedHtml.getElementsByName(domName)[0].setAttribute('value', '1234567890');
// now i need to replace current update data entire screen
document.write(parsedHtml)
谢谢,
Gopal.R
Bcoz document.write accept only string.
使用 document.write
几乎总是不好的做法。
但是,如果出于某种原因你真的需要,你从 parseFromString
得到的是 Document
object。它有一个 documentElement
,你可以得到 innerHTML
或 outerHTML
of:
document.write(parsedHtml.documentElement.innerHTML);
// or
document.write(parsedHtml.documentElement.outerHTML);
实例:
const domName = 'MOBILE_NO';
// Below dom was getting from api.
const dom = '<html><head><title>Merchant Checkout Page</title></head><body><center><h1>Please do not refresh this page...</h1></center><form method="post" name="paytm_form"><input type="hidden" name="MOBILE_NO" value="xxxxxxxx"></form></body></html>';
const parser = new DOMParser();
const parsedHtml = parser.parseFromString(dom, 'text/html');
parsedHtml.getElementsByName(domName)[0].setAttribute('value', '1234567890');
// now i need to replace current update data entire screen
document.write(parsedHtml.documentElement.innerHTML);
但是,再一次,可能最好只是附加到页面,例如
document.body.appendChild(parsedHtml.documentElement);
实例:
const domName = 'MOBILE_NO';
// Below dom was getting from api.
const dom = '<html><head><title>Merchant Checkout Page</title></head><body><center><h1>Please do not refresh this page...</h1></center><form method="post" name="paytm_form"><input type="hidden" name="MOBILE_NO" value="xxxxxxxx"></form></body></html>';
const parser = new DOMParser();
const parsedHtml = parser.parseFromString(dom, 'text/html');
parsedHtml.getElementsByName(domName)[0].setAttribute('value', '1234567890');
document.body.appendChild(parsedHtml.documentElement);
或者遍历它并附加它的 children:
let child = parsedHtml.documentElement.firstChild;
while (child) {
let next = child.nextSibling;
document.documentElement.appendChild(child);
child = next;
}
实例:
const domName = 'MOBILE_NO';
// Below dom was getting from api.
const dom = '<html><head><title>Merchant Checkout Page</title></head><body><center><h1>Please do not refresh this page...</h1></center><form method="post" name="paytm_form"><input type="hidden" name="MOBILE_NO" value="xxxxxxxx"></form></body></html>';
const parser = new DOMParser();
const parsedHtml = parser.parseFromString(dom, 'text/html');
parsedHtml.getElementsByName(domName)[0].setAttribute('value', '1234567890');
let child = parsedHtml.documentElement.firstChild;
while (child) {
let next = child.nextSibling;
document.documentElement.appendChild(child);
child = next;
}
我有一些 HTML 字符串。使用 domparser 更新一些值,现在我需要返回 HTML 具有更新值的字符串格式... Bcoz document.write 只接受字符串。
检查示例补丁,
const domName = 'MOBILE_NO';
// Below dom was getting from api.
const dom = '<html><head><title>Merchant Checkout Page</title></head><body><center><h1>Please do not refresh this page...</h1></center><form method="post" name="paytm_form"><input type="hidden" name="MOBILE_NO" value="xxxxxxxx"></form></body></html>';
const parser = new DOMParser();
const parsedHtml = parser.parseFromString(dom, 'text/html');
parsedHtml.getElementsByName(domName)[0].setAttribute('value', '1234567890');
// now i need to replace current update data entire screen
document.write(parsedHtml)
谢谢,
Gopal.R
Bcoz document.write accept only string.
使用 document.write
几乎总是不好的做法。
但是,如果出于某种原因你真的需要,你从 parseFromString
得到的是 Document
object。它有一个 documentElement
,你可以得到 innerHTML
或 outerHTML
of:
document.write(parsedHtml.documentElement.innerHTML);
// or
document.write(parsedHtml.documentElement.outerHTML);
实例:
const domName = 'MOBILE_NO';
// Below dom was getting from api.
const dom = '<html><head><title>Merchant Checkout Page</title></head><body><center><h1>Please do not refresh this page...</h1></center><form method="post" name="paytm_form"><input type="hidden" name="MOBILE_NO" value="xxxxxxxx"></form></body></html>';
const parser = new DOMParser();
const parsedHtml = parser.parseFromString(dom, 'text/html');
parsedHtml.getElementsByName(domName)[0].setAttribute('value', '1234567890');
// now i need to replace current update data entire screen
document.write(parsedHtml.documentElement.innerHTML);
但是,再一次,可能最好只是附加到页面,例如
document.body.appendChild(parsedHtml.documentElement);
实例:
const domName = 'MOBILE_NO';
// Below dom was getting from api.
const dom = '<html><head><title>Merchant Checkout Page</title></head><body><center><h1>Please do not refresh this page...</h1></center><form method="post" name="paytm_form"><input type="hidden" name="MOBILE_NO" value="xxxxxxxx"></form></body></html>';
const parser = new DOMParser();
const parsedHtml = parser.parseFromString(dom, 'text/html');
parsedHtml.getElementsByName(domName)[0].setAttribute('value', '1234567890');
document.body.appendChild(parsedHtml.documentElement);
或者遍历它并附加它的 children:
let child = parsedHtml.documentElement.firstChild;
while (child) {
let next = child.nextSibling;
document.documentElement.appendChild(child);
child = next;
}
实例:
const domName = 'MOBILE_NO';
// Below dom was getting from api.
const dom = '<html><head><title>Merchant Checkout Page</title></head><body><center><h1>Please do not refresh this page...</h1></center><form method="post" name="paytm_form"><input type="hidden" name="MOBILE_NO" value="xxxxxxxx"></form></body></html>';
const parser = new DOMParser();
const parsedHtml = parser.parseFromString(dom, 'text/html');
parsedHtml.getElementsByName(domName)[0].setAttribute('value', '1234567890');
let child = parsedHtml.documentElement.firstChild;
while (child) {
let next = child.nextSibling;
document.documentElement.appendChild(child);
child = next;
}