【问题标题】:How can I include an html file in my page by clicking on a button ?如何通过单击按钮在我的页面中包含 html 文件?
【发布时间】:2016-12-11 20:50:52
【问题描述】:

我正在尝试使用一个简单的按钮获取一个页面,该按钮将在单击时加载另一个 html 页面的内容。

<input type="button" value="Create a square" id="send" onClick="createaquare()" />

我想要的是,当我单击按钮时,该函数会创建一个称为加载另一个包含简单正方形的 html 文件的正方形。

    <div></div>

<style type="text/css">

div
{
    height : 200px;
    width : 200px;
    background: red;
    opacity : 0.3;
}

每次我点击按钮时,都会出现另一个方块。使用 javascript 或 jquery、ajax 或任何东西怎么可能?

谢谢

【问题讨论】:

  • 是否必须加载另一个页面或者按钮单击可以简单地生成一个新方块?
  • 是的,我只是想生成一个新方块,但我认为这样做更好,以避免在页面最初加载时加载方块

标签: javascript html button include


【解决方案1】:

与其引发另一个 HTTP 调用以及必须获取始终相同的文件内容的复杂性,不如仅在单击按钮时生成方块。

通过为所有新 div 赋予其自己的 CSS 类,您可以对它们进行相同的样式设置,同时保留所有原始 div。

为了清楚起见,在我的示例中,我向每个新方块添加相同的内容,但如果您要向点击事件处理程序添加条件逻辑,您可以根据自己的逻辑在不同的方块中创建不同的内容.

如果您想要添加的只是纯文本,则使用 textContent 属性将内容注入到正方形中,如果您想要注入需要解析的内容(即您的内容包含标记 - 用于例如,当我将文本“ESPN”添加到链接时,我还添加了一个 HTML &lt;br&gt; 元素)。

var btn = document.getElementById("btnMakeSquare");

var counter = 1;

btn.addEventListener("click", function(){

  // Create a new square and configure it:
  var newSquare = document.createElement("div");
  newSquare.id = "newDiv" + counter;
  newSquare.textContent = newSquare.id;
  newSquare.setAttribute("class", "newDiv");
  
  // Now create content for that square (this can be anything)
  var newLink = document.createElement("a");
  newLink.href = "http://espn.com";
  newLink.innerHTML = "<br>ESPN";
  newLink.id = "link" + counter;
  
  // Put the link into the square:
  newSquare.appendChild(newLink);
  
  // Bump up the counter:
  counter++;
  
  // Inject the square (which now contains a link) into the DOM:
  document.body.appendChild(newSquare);
  

});
/* Only the one original div will be affected by this: */
#originalDiv {
    height : 50px;
    width : 50px;
    background: red;
    opacity : 0.3;

}

/* All generated divs will have the following class 
   so they will all look the same. But, none of the
   original divs will have this class, so they won't
   be affected.                                      */
.newDiv {
    height : 75px;
    width : 75px;
    background: blue;
    border:1px solid black;
    font-weight:bold;
    font-size:.1.5em;
    color:yellow;
    text-align:center;
}

/* This only affects links in new squares */
.newDiv > a{
  color:green;

}
<input type="button" value="Create a square" id="btnMakeSquare">
<div id="originalDiv">I'm the original div</div>

【讨论】:

  • 是否应该 div 选择器 css 引用动态 counter ids,以排除将 css 处的样式应用到 document 中的所有 div 元素?
  • 如果你不希望所有的新方块看起来像原来的 div,只需将 CSS 选择器更改为原来的div 元素的id,那么它将只适用于那个。让我更新答案以反映这一点。
  • 感谢您的回答,非常清楚和完整。但是如果将来,我想在方块中插入很多元素,这个解决方案还有用吗?我的意思是,我想创建包含 php 变量值的方块,例如。我知道这不是我最初的问题,但这是让我认为最好包含另一个文件的原因之一,这样我就可以同时包含正方形中的每个元素。
  • 您可以使用您希望的任何其他内容填充任何内容。我将再次更新答案以表明这只是嵌套元素的问题。
  • 是的,这绝对是最好的解决方案。我会尝试在我的代码中使用它。非常感谢!
【解决方案2】:

您可以使用&lt;iframes&gt; 在您的页面上加载另一个网页。您可以使用 Javascript 不断在彼此之间添加&lt;iframes&gt; 的页面,从而创建您想要的效果。 W3Schools 有一篇文章介绍了如何使用&lt;iframes&gt;。

编辑:请注意,这绝对不是完成手头任务的最佳方式。使用 CSS 和 Javascript 可以实现相同的效果,而无需加载多个网页。

【讨论】:

  • 这个问题似乎有点矫枉过正。
  • 我在回答这个问题,但我绝对同意这不是解决这个问题的最佳方式。我将对此添加评论。
  • 我试过这个,但它并没有真正给我一个好的结果。每个方块都在 iframe 创建的另一个边框中。
  • 查看关于它不是完成任务的好方法的评论。但是您的问题要求能够加载到另一个网页中,这将使您能够做到。
  • 这是真的,但我已经测试过 iframe,这似乎不是一个好的解决方案。说真的,我在想像 ajax 这样的东西。
猜你喜欢
  • 2014-07-03
  • 1970-01-01
  • 1970-01-01
  • 2016-03-30
  • 2016-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多