Py学习  »  Jquery

使用jQuery替换元素

snowflakes74 • 3 年前 • 1349 次点击  

我在powerapps门户中呈现了以下HTML,我想替换所有出现的

    <font size = 3>..... </font>

具有

    <div class="legend">... </div> 

我尝试了下面的代码片段,但它没有取代它:

    var $descBox = $("<font size = 3>"); $descBox.replaceAll("<div class='legend well'>");
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133368
文章 [ 1 ]  |  最新文章 3 年前
Tomalak
Reply   •   1 楼
Tomalak    3 年前
  • 要搜索现有元素,请在jQuery中使用CSS表达式 $() .
  • 要创建新元素,请在jQuery中使用HTML代码 $() .
  • .replaceAll() 是一个字符串函数。你是说 .replaceWith() .

$("font[size=3]").replaceWith( $("<div class='legend well'>") );

或者更短

$("font[size=3]").replaceWith("<div class='legend well'>");

只交换 <font> 元素 没有 此外,更换内容还需要几个步骤。

$("font[size=3]").each(function () {
  // insert new container div after each `<font>`
  var $div = $("<div class='legend well'>").insertAfter(this);

  // remove the `<font>` and append its children to the new container
  $(this).remove().children().appendTo($div);
});
div.legend {
   color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<font size="3">
  <p>elements to keep</p>
</font>
<font size="3">
  <p>more elements to keep</p>
</font>