简体   繁体   English

jQuery replaceWith() 不替换新值

[英]jQuery replaceWith() not replace new value

I have simple html code with 2 button and script to replace element when click each button.我有简单的 html 代码,带有 2 个按钮和脚本,用于在单击每个按钮时替换元素。 When I click button 1 it replace right way as expected.当我单击按钮 1 时,它按预期替换正确的方式。 But continue click button 2 it still replace old value which I have when click button 1.但是继续单击按钮 2 它仍然会替换单击按钮 1 时的旧值。

Here is my code:这是我的代码:

 jQuery(document).ready(function() { $("button[name=btn-add]").on('click', function(e) { e.preventDefault(); let Button_Id = $(this).attr("id") console.log(Button_Id); let test_content = $("#div-clone"); stri = '<div>' + Button_Id + '</div>'; test_content.find('#hello').replaceWith(Button_Id); test_content = test_content.html(); console.log(test_content); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <button type="button" class="btn btn-outline-success" name="btn-add" id="1"> <i class="fas fa-plus fa-1x"></i> <span style="padding-left:3px;"> button 1 </span> </button> <button type="button" class="btn btn-outline-success" name="btn-add" id="2"> <i class="fas fa-plus fa-1x"></i> <span style="padding-left:3px;"> button 2 </span> </button> <div name="div_test" id="div-clone" style="display:none"> <span id="hello"> hello </span> </div>

When click button 1 and then click button 2:单击按钮 1 然后单击按钮 2 时:

result:结果:

// 1 1 // 2 1 // 1 1 // 2 1

expect:预计:

// 1 1 // 2 2 // 1 1 // 2 2

You cannot find the element with the id of 'hello' on the second click because its already been removed.您在第二次单击时找不到 id 为'hello'的元素,因为它已被删除。 But you can modify your code to add a new div with the sane id again.但是您可以修改您的代码以再次添加一个具有健全 id 的新 div。 So you replace <div id="hello">hello</div> with <div id="hello">1</div> or <div id="hello">2</div> depending on the button id that you clicked:因此,您将<div id="hello">hello</div>替换为<div id="hello">1</div><div id="hello">2</div>取决于按钮 id你点击了:

 jQuery(document).ready(function() { $("button[name=btn-add]").on('click', function(e) { e.preventDefault(); let Button_Id = $(this).attr("id") console.log(Button_Id); let test_content = $("#div-clone"); stri = '<div id="hello">' + Button_Id + '</div>'; test_content.find('#hello').replaceWith(stri); test_content = test_content.text(); console.log(test_content); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <button type="button" class="btn btn-outline-success" name="btn-add" id="1"> <i class="fas fa-plus fa-1x"></i> <span style="padding-left:3px;"> button 1 </span> </button> <button type="button" class="btn btn-outline-success" name="btn-add" id="2"> <i class="fas fa-plus fa-1x"></i> <span style="padding-left:3px;"> button 2 </span> </button> <div name="div_test" id="div-clone" style="display:none"> <span id="hello"> hello </span> </div>

The problem in your code was you were replacing the div, so basically when you click on the second time the " hello " div is not there instead of replacing you can change the text inside div like below您的代码中的问题是您正在替换 div,所以基本上当您第二次单击时,“ hello ” div 不存在而不是替换,您可以更改 div 内的文本,如下所示

 jQuery(document).ready(function() { $("button[name=btn-add]").on('click', function(e) { e.preventDefault(); let Button_Id = $(this).attr("id") $("#div-clone").find('#hello').text(Button_Id); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <button type="button" class="btn btn-outline-success" name="btn-add" id="1"> <i class="fas fa-plus fa-1x"></i> <span style="padding-left:3px;"> button 1 </span> </button> <button type="button" class="btn btn-outline-success" name="btn-add" id="2"> <i class="fas fa-plus fa-1x"></i> <span style="padding-left:3px;"> button 2 </span> </button> <div name="div_test" id="div-clone"> <span id="hello"> hello </span> </div>

Note笔记

If you wanted to add the element you can use like below, so eventhough it replaces the next time when you are clicking we are replacing with a div of id " hello " so it will be able to find it out from the dom.如果你想添加你可以像下面这样使用的元素,所以即使它在你下次单击时替换,我们也会替换为 id 为“ hello ”的 div,这样它就可以从 dom 中找到它。

$("#div-clone").find('#hello').text('<div id="hello">' + Button_Id + '</div>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM