javascript - 如何在动态构建的 html 中包含 weatherwidget.io
问题描述
我正在尝试将 weatherwidget.io 的小部件添加到我的网站,但需要根据用户当前位置动态使用字符串。例如,我将包含一个硬字符串,但知道我会根据位置拉出字符串。
this.weatherwidget = '
<div><a class="weatherwidget-io" href="https://forecast7.com/en/38d91n77d04/washington/?unit=us" data-label_1="Washington Monument" data-label_2="National Mall" data-font="Arimo" data-theme="sky">Washington Monument National Mall</a>
<script>
! function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (!d.getElementById(id)) {
js = d.createElement(s);
js.id = id;
js.src = "https://weatherwidget.io/js/widget.min.js";
fjs.parentNode.insertBefore(js, fjs);
}
}(document, "script", "weatherwidget-io-js");
</script>';
我确实读过 HTML 5 不允许嵌入脚本,我尝试了我能找到的唯一其他解决方案,即将它嵌入到 img onload 事件中,如下所示:
<img src='empty.gif' onload='"https://forecast7.com/en/38d91n77d04/washington/?unit=us" data-label_1="Washington Monument" data-label_2="National Mall" data-font="Arimo" data-theme="sky" >Washington Monument National Mall</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="https://weatherwidget.io/js/widget.min.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","weatherwidget-io-js");</script>';
但这似乎也不起作用。如何动态构建包含脚本的 hmtl?
解决方案
推荐阅读
- c# - Skybrud 社交 Instagram 停止在网站上工作
- amazon-web-services - AWS CloudWatch 规则在删除和创建别名后无法触发 lambda 函数
- ionic2 - 如何修复离子中的“cordova平台添加android”错误
- javascript - FormArray(在 Angular6 中):返回空数组
- jenkins - 使用凭据插件运行 Jenkins 流水线步骤的问题
- perl - 使用 or 运算符对方法调用进行 perlttidy 缩进
- angular - 如何正确退订 Observable Angular 7?
- r - 通过线性插值查找缺失值(时间序列)
- html - 当我在 localhost:5000 运行它时,我的 html 站点中的所有文本都作为链接出现
- distributed-computing - Hazelcast 3.11 客户端连接重试配置问题 -