gpt4 book ai didi

javascript - React useEffect 附加脚本未触发

转载 作者:行者123 更新时间:2023-11-30 19:16:00 25 4
gpt4 key购买 nike

我正在尝试加载一个脚本,该脚本一旦被触发就会生成一个 iframe 和一个天气小部件。我可以正确地附加脚本,但它不会被调用,也不会生成 iframe

我试过直接在渲染中添加脚本,它会短暂加载 iframe 然后切换回脚本标签

const City = ({ content, schema }) => {
useEffect(() => {
let script = document.createElement("script");
script.src =
"https://darksky.net/widget/graph-bar/32.7174,-117.1628/us12/en.js?width=100%&height=400&title=Full Forecast&textColor=333333&bgColor=FFFFFF&transparency=false&skyColor=undefined&fontFamily=Default&customFont=&units=us&timeColor=333333&tempColor=ff8200&currentDetailsOption=true";
script.async = true;

document.getElementById("widget").appendChild(script);
}, [])

return (<div id="widget"></div>);
};

export default City;

最佳答案

注意控制台中显示的警告:

Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.

参见 Execute write on doc: It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.

关于javascript - React useEffect 附加脚本未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58000895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com