【问题标题】:How to select and load the content of external page如何选择和加载外部页面的内容
【发布时间】:2021-11-28 19:56:11
【问题描述】:

在下面的示例中,我有一个选择框,对于所做的每个选择,都会显示一个链接

function AbrirSecao(secao) {
      let display = document.getElementById('content')
      display.textContent = secao
     }


<select name="unidade" id="unidade" onChange="AbrirSecao(this.value)">
  <option value="">Selecione sua Cidade</option>
  <option value="http://www.google.com.br#one">Sua Cidade 1</option>
  <option value="http://www.google.com.br#two">Sua Cidade 2</option>
</select>
<p id="content"></p>

相反,我需要在选择选项时页面会加载外部页面的内容,如下所示:

<h1>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus vehicula 
   tortor metus, laoreet condimentum urna aliquet vitae. Aliquam eu felis 
   malesuada, maximus risus nec, aliquet leo</p>

【问题讨论】:

    标签: javascript html css select


    【解决方案1】:

    除非您要从中加载数据的网站与您的网站在同一个域中,否则您将被 CORS 政策阻止。

    如果您可以控制需要从中获取内容的网站服务器,则可以使用 fetch():

    https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

    有关 CORS 的更多信息:https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

    这是一个使用 fetch() https://jsfiddle.net/v4uL0na2/ 的示例第二个选项从 JSFiddle 主页返回 HTML。由于 CORS,第二个选项会导致错误。

        function AbrirSecao(secao) {
          let display = document.getElementById('content')
    
          fetch(secao).then(function(response) {
            // The API call was successful!
            return response.text();
          }).then(function(html) {
            // This is the HTML from our response as a text string
            var parser = new DOMParser();
            var doc = parser.parseFromString(html, 'text/html');
            const element = doc.querySelector('body > *');
            display.appendChild(element);
          }).catch(function(err) {
            // There was an error
            console.warn('Something went wrong.', err);
          });
        }
    <select name="unidade" id="unidade" onChange="AbrirSecao(this.value)">
      <option value="">Selecione sua Cidade</option>
      <option value="/">jsfiddle</option>
      <option value="http://www.google.com.br#two">Sua Cidade 2</option>
    </select>
    <p id="content"></p>

    (上面的代码示例在这里不起作用)

    【讨论】:

    • 它们是同一站点的子页面。除了获取之外,还有其他方法可以做到这一点吗?
    • Fetch 似乎是正确的工具。抓取有问题吗?
    • 没问题,但是如何在select中适配这个呢?
    • 我刚刚更新了答案,包括一个例子。
    • 我设法在网站上实现了它,但是当我在浏览器中运行它时,会出现一条错误消息,指出不支持 URL 方案“文件”。这个过程只有在服务器上运行才有效吗?
    【解决方案2】:

    使用 Jquery 使用 load(),很简单..

    $('#siteloader').load('http://www.your-link.com');
    
    function AbrirSecao(secao) {
      $('#content').load(secao);
    }
    

    vanillaJS 使用 fetch。试试这个:

    function AbrirSecao(secao) {
      fetch(secao)
        .then(function(response) {
          return response.text();
        })
        .then(function(body) {
          document.querySelector('#content').innerHTML = body;
        });
    }
    

    【讨论】:

    • 如何更改部分并切换到子域?例如,我在主页 your-link.com 上选择不选择选项,一旦选择,将下面的部分加载到子域 your-link.com/suddomain
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多