如何将存储在JS中的变量中的URL传递给HTML文件?

发布于 2025-01-17 19:41:47 字数 1096 浏览 1 评论 0原文

我是初学者,所以我敢肯定这可能很明显。

我有一个API呼叫,该呼叫找到了我存储在一个称为senterurl的变量中的位置的当前新闻。我希望在模式中显示我的HTML文件。

这是我的模态的代码,

<div id="newsModal" class="modal">   
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-heading">News</h4>
          <button type="button" class="btn-close"  aria-label="Close"></button>
      </div>
      <div class="modal-body">
          <table class="table table-striped">
    <tbody>
     
        <tr>
          
          <td><p id="txtNews"></p></td>

        </tr>
        
      
     
  </table>
  </div>
        </div>
      </div>

我在JS文件中尝试了以下代码,

newsUrl = result['data'][0]['url'];
          newsImage = result['data'][0]['image'];
          newsTitle = result['data'][0]['title'];
          

          newsLink = ('<a href="//'+newsUrl+'" target=_blank ></a>');
             
          $('#txtNews').html(newsLink);

没有错误。

I am a beginner so I'm sure this is probably obvious.

I have an API call which finds current news for a location which I have stored in a variable called newsUrl. I wish to show this in my HTML file within a modal.

here is the code for my modal

<div id="newsModal" class="modal">   
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-heading">News</h4>
          <button type="button" class="btn-close"  aria-label="Close"></button>
      </div>
      <div class="modal-body">
          <table class="table table-striped">
    <tbody>
     
        <tr>
          
          <td><p id="txtNews"></p></td>

        </tr>
        
      
     
  </table>
  </div>
        </div>
      </div>

I have tried the following code within my JS file

newsUrl = result['data'][0]['url'];
          newsImage = result['data'][0]['image'];
          newsTitle = result['data'][0]['title'];
          

          newsLink = ('<a href="//'+newsUrl+'" target=_blank ></a>');
             
          $('#txtNews').html(newsLink);

No errors in the console.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白衬杉格子梦 2025-01-24 19:41:47

尝试使用:$('#txtNews').append(newsLink);

Try to use: $('#txtNews').append(newsLink);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文