[英]Can I use Element.insertAdjacentHTML() to insert tag script
i have request to node.js from client.我有来自客户端的 node.js 请求。 Node.js response string
Node.js 响应字符串
app.post('/verify',cors(issue2options), async (req, res) => {
let auth = await mongo({
input:'express app',
data:req.body['auth']['address'],
type:'verify'
},'get', 'type')
if(empty(auth)){
res.send(JSON.stringify(`
<div id="registration" slot="varan-about">
Войдите под аккаунтом администратора
<script type="module">
console.log('sssss')
<\/script>
</div>`))
}else{
res.send(true)
}
}) On client I insert string to document['body'] }) 在客户端我将字符串插入到文档['body']
document['body'].insertAdjacentHTML('afterbegin', `${json}`);
But then i insert string script no work.但后来我插入字符串脚本没有工作。
Is it possible to insert a script using this method ?是否可以使用此方法插入脚本?
From the official HTML5 spec :来自官方 HTML5 规范:
script elements inserted using innerHTML do not execute when they are inserted.
使用 innerHTML 插入的脚本元素在插入时不会执行。
Inserting the adjacent HTML is the same as modifying innerHTML
of a specific DOM element.插入相邻的 HTML 与修改特定 DOM 元素的
innerHTML
相同。
If you really want to provide a mixture of html, css and javascript back from your server, you might want to put each part into a respective property.如果您真的想从服务器返回 html、css 和 javascript 的混合,您可能希望将每个部分放入相应的属性中。 For example:
{"js": "<Some js here">, "html": "<Some markup here>", "css": "<Some styles here>"}
Then, on the client side, you can create a script tag and inline your logic to it:例如:
{"js": "<Some js here">, "html": "<Some markup here>", "css": "<Some styles here>"}
然后,在客户端,你可以创建一个脚本标签并将你的逻辑内联到它:
var script = document.createElement('script');
script.textContent = data.js;
document.body.appendChild(script);
The style could be handled in the similar fashion:样式可以以类似的方式处理:
var style = document.createElement('style');
style.textContent = data.css;
document.head.appendChild(style);
Finally, the markup could be changed via innerHTML
but keep in mind that this will not remove any event handlers attached to the DOM elements that will have been replaced by new elements after parsing new HTML:最后,可以通过
innerHTML
更改标记,但请记住,这不会删除任何附加到 DOM 元素的事件处理程序,这些事件处理程序在解析新 HTML 后将被新元素替换:
var someNode = document.querySelector('<some selector>');
someNode.innerHTML = data.html;
As many of the answers have pointed out, attempting to insert the script directly as text will prevent it from running.正如许多答案所指出的那样,尝试将脚本直接作为文本插入会阻止它运行。 Here is one method that works:
这是一种有效的方法:
const divFragment = document.createRange().createContextualFragment(`${json}`);
document.body.prepend(divFragment);
Sources:资料来源:
Yes you can user insertAdjacentHTML
with any是的,您可以使用任何用户
insertAdjacentHTML
for more infformation visit this page有关更多信息,请访问此页面
https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML
in you implementation, I think you don't have to create DOM on server side, just get the response and create DOM on client side in JS在你的实现中,我认为你不必在服务器端创建 DOM,只需在 JS 中获取响应并在客户端创建 DOM
like喜欢
var p = document.createElement("p"); p.text = 'My new Paragraph :)' function myFunction() { var h = document.getElementById("myH2"); h.insertAdjacentHTML("afterend", p.text); }
<h2 id="myH2">My Header</h2> <button onclick="myFunction()">Insert a paragraph</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.