javascript - PHP 中包含模板时删除旧内容

标签 javascript php jquery

<?php include "main.php" #this file is supposed to be included after an event occurred ?>

 <!-- this should be deleted after include, also styles scripts and everything in this old-content id-->
         <div id="old-content">
         <link rel="stylesheet" href="custom.css">
    some content
        </div>

我尝试过,我在旧内容 div 中添加了以下内容,但 jquery 仅包含在模板 main.php

$("#old-content").empty();

Problem with this, is that files are not actually removed and when i view page source i can see the old content at the end

最佳答案

您可以使用.remove()方法从 DOM 中取出元素。当您想要删除元素本身及其内部的所有内容时,请使用它。

$("#old-content").remove();

// find elements
var button = $("button")

// handle click and add class
button.on("click", function(){
  $("#banner-message").remove();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="banner-message">
  <p>Hello World</p>
  <button>Remove</button>
</div>

关于javascript - PHP 中包含模板时删除旧内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49550589/

相关文章:

javascript - 访问要在 React Native Text 组件中显示的对象数组

javascript - 如何将iframe内容复制到div?

php - 为什么我网站上的所有 html 表单都禁用了 Internet Explorer 自动完成功能?

php - 设置根据 MySQL 值选择的下拉列表

php - 检查是否启用了 Javascript

javascript - 通过 jQuery 附加的 HTML 不会接受点击事件

Jquery 验证依赖字段

javascript - 论文js限制总路径长度

javascript - 如何从字符串中查找EJS标签变量名?

php - PayPal IPN - PHP 和 MySQL