javascript - 使用 Javascript 将段落从外部 HTML 页面插入到另一个 HTML 页面

标签 javascript jquery html ajax json

我是 JavaScript 新手,所以如果我想做的事情没有任何意义,请原谅。我想要做的是将一个段落和一个标题从外部 HTML 页面导入到另一个 HTML 页面,这可以使用 JavaScript 实现吗?如果是,您能否指出我在哪里可以找到正确的信息。

将要完成导入的页面。

  <ul>
        <li>
          <h3>Header imported form page 1</h3>
          <p>Paragraph imported from page 1</p>
        </li>

        <li>
          <h3>Header imported form page 2</h3>
          <p>Paragraph imported from page 2</p>
        </li>
  </ul>

外部页面 1

  <h3>Header in page 1</h3>
  <p>Paragraph in page 1</p>

外部页面 2

  <h3>Header in page 2</h3>
  <p>Paragraph in page 2</p>

我搜索过,但我找到的大多数帖子都是导入完整的 HTML 页面。

仅供引用 - 我无法使用 PHP,因为我无权访问服务器。

谢谢

最佳答案

您可以使用 jQuery load()方法。

The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.

例如:

 $( "#result" ).load( "ajax/test.html #container" );

When this method executes, it retrieves the content of ajax/test.html, but then jQuery parses the returned document to find the element with an ID of container. This element, along with its contents, is inserted into the element with an ID of result, and the rest of the retrieved document is discarded.

关于javascript - 使用 Javascript 将段落从外部 HTML 页面插入到另一个 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26388789/

相关文章:

javascript - 通过 JavaScript 重启 CSS 动画

javascript - 这是否被视为 POST 或 GET 请求?

javascript - d3 直方图和条形图数据

javascript - 为什么 parseInt() 有时会返回字符串?

JavaScript 错误 "Cannot read property"

jquery - 通过索引检索具有类的某些元素

javascript - 使用 Jquery 显示 body 背景图像

jquery - 使用 JQuery 从中心动画增长

javascript - 用于两个不同文本区域的两个复制按钮

Javascript form.submit() 失败并停止执行其余的 javascript 代码