【问题标题】:Jquery How to replace HTML after specific element?Jquery如何在特定元素之后替换HTML?
【发布时间】:2021-06-15 16:06:22
【问题描述】:

我有一个很简单的布局,部分如下:

<td class="subset">
<form name="searchFrm" method="get" action="update.php">
<input type="text" name="search">
</form>

html 
html
html

</td>

我想用从 PHP 页面加载的新 html 替换 formtd 之后的所有内容。

例如: 将其更改为:

<td class="subset">
<form name="searchFrm" method="get" action="update.php">
<input type="text" name="search">
</form>

New HTML from PHP

</td>

我知道我可以使用$( '[name="searchFrm"]' ) 选择表单并使用.load('new.php') 加载外部页面,但是如何删除表单后的 HTML,然后添加新的 html?

我试过了

$( '[name="searchFrm"]' ).after().load('new.php')

这没有帮助,有什么办法可以做到这一点? 我不是要添加或删除单个行,而是将元素后的 HTML 替换为一行。

使用正确的表单名称更新帖子。

谢谢

【问题讨论】:

  • 表格前面也可以有你要保留的内容吗?
  • 为什么不将表单后面的内容包装在一个带有 id 的元素中,以便您可以直接定位它?
  • @GabrielePetrioli - 在这个特定的td 中,form 之前没有任何内容。在这一行之前和之后还有其他行。我无法控制某些代码,因此无法添加新元素等。
  • 这能回答你的问题吗? Add/remove HTML inside div using JavaScript
  • 嗨,检查this 可能会有所帮助。

标签: jquery


【解决方案1】:

首先请注意,$('[name="search"]') 不仅会选择form,还会选择input,因为它也具有相同的name 值。所以使用$('form[name="search"]')

有几种方法可以解决您的问题。

一个是answer of @pavel,它会随新内容一起重写表单html。

这种方法的一些问题是

  • 确切发布的代码还受到选择器中缺少form 的影响,因此将从结果中删除form 标记。
  • 如果您稍后在 form 之前添加一些内容(在同一个 td)它将停止工作(它将删除该内容
  • 如果表单或其任何后代上有附加的处理程序,它们将会丢失,因为.html 只保留 html 文本,而不是通过代码附加的任何内容。
  • 它也不能直接使用.load。但是您可以使用$.ajax 解决这个问题

另一个更复杂但更安全的解决方案是在form 之后经历选择实际节点的痛苦。

为此,您需要使用.contents,这是唯一一个也返回textNodes 的jQuery 方法。

一个详细的实现(你可以看到每个步骤)可能是

function replaceContentsAfterForm(newUrl) {
  const form = $('form[name="search"]'); // select the form only
  const formParent = form.parent(); // get the form container
  const contents = [...formParent.contents()]; // select the contents of the container in an array
  const formIndex = contents.indexOf(form[0]); // find where the form is in the array 
  const afterForm = contents.slice(formIndex + 1); // select everything after the form
  const placeholder = $('<div id="placeholder" />'); // create a temporary placeholder to contain the data after the form
  placeholder.append(afterForm); // fill the placeholder with the contents after the form
  form.after(placeholder); // put the placeholder after the form in the DOM

  $(placeholder).load(newUrl, function() { // load the new content
    placeholder.replaceWith(placeholder.contents()) // and finally remove the placeholder eleemnt
  })
}

// and you can call it with
replaceContentsAfterForm('new.php');

【讨论】:

  • 我发现我发布的代码中有一个错字。该表单实际上称为searchFrm 我已将代码更新为const form = $('form[name="searchFrm"]'); // select the form only 当我运行代码时,我得到Uncaught TypeError: formParent.contents is not a function or its return value is not iterable
  • @Tom 你用的是什么 jquery 版本?你能记录下formParentformParent.contents() 返回的内容吗?
  • 我正在运行 v1.8.0 :( 我没有意识到它已经那么旧了。要运行它需要什么版本?
  • @Tom 它至少需要 2.2,因为那是添加迭代器的时候。您也可以将const contents = [...formParent.contents()]; 替换为const contents = Array.from(formParent.contents()); 示例jsfiddle.net/gaby/bqLpk4vc/5
【解决方案2】:

通过form + new content更改父元素内容。

var form = $('[name=search]');
var parent = form.parent();

parent.html(form.html() + 'new HTML content');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<td class="subset">
    <form name="search" method="get" action="update.php">
        <input type="text" name="search">
    </form>

    html 
    html
    html

</td>

【讨论】:

  • 这将删除任何附加到表单或其任何后代的处理程序。
  • @pavel - 我如何加载新的 html ?我通常会使用.load('new.php')
猜你喜欢
  • 2017-09-21
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2017-08-13
  • 1970-01-01
相关资源
最近更新 更多