我正在尝试使用 Javascript 动态地将元素放入其他元素中,而无需刷新页面,它的 AJAX 部分可以工作并且功能正常。然而,由于某种未知的原因,我的代码自动关闭。
这是代码片段,您可以看到它实际上并未关闭。但是在浏览器中运行代码后它被关闭
HTML
<div id="Events">
JavaScript
Get = document.getElementById("Events");
Get.innerHTML = "<div class='large-6 columns Pages' id='Page" + PN + "' style='background-color: #" + i + i + i + ";'>";
Get.innerHTML = Get.innerHTML + "<div class='large-6 columns Pages' id='Page" + PN + "' style='display: none; background-color: #" + i + i + i + ";'>";
页面源码中的结果为:
<div id="Page1" class="large-6 columns Pages" style="background-color: #000;"></div>
<div class="EventsClass"></div>
正如您所看到的,这是一个问题,因为我试图将元素放入元素中。但是由于关闭标签我不能。
我已经搜索了几个小时,找不到解决方案,甚至找不到原因。
没有关闭标签,但它会自动关闭。有没有办法覆盖这个?还是绕过它?
来自文档:
The innerHTML
属性设置或返回元素的 HTML 内容(内部 HTML)。
显然,该属性返回的内容必须是格式良好的HTML 并且肯定会由带有结束标记的浏览器呈现。
如果您想在元素内使用元素并更新HTML
你想要的GET
目的。只需根据要添加的内容创建一个普通的字符串变量,然后对其进行清理,当您拥有所需的完整内容时,然后更新.innerHTML
类似的东西:
//content is a variable that just holds the string representing the HTML Content
var content = "<div class='large-6 columns Pages' id='Page" + PN + "' style='background-color: #" + i + i + i + ";'>";
content += "<div class='large-6 columns Pages' id='Page" + PN + "' style='display: none; background-color: #" + i + i + i + ";'>";
//close the divs or add more elements to the variable content
Get.innerHTML = content; //At the end.
我希望这能让您朝着正确的方向开始。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)