首页 > 解决方案 > 如何在动态构建的 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?

标签: javascriptjqueryhtml

解决方案


推荐阅读