简体   繁体   English

jquery ReplaceWith 不使用 2 个按钮

[英]jquery ReplaceWith not working with 2 buttons

I have created 2 buttons, each button will replace content based on partial views, I could load the partial view on the page when I click the button, but this works only once, for instance I clicked button-1 and loaded data, now if I click on button 2 its not working, I needed to go back to main page to click again on button-2我创建了 2 个按钮,每个按钮将根据部分视图替换内容,当我单击按钮时,我可以在页面上加载部分视图,但这只能工作一次,例如我单击 button-1 并加载数据,现在如果我单击按钮 2 它不起作用,我需要 go 返回主页以再次单击按钮 2

    <h3>
       <a class="btn btn-warning" id="button1"> Partial View 1</a>
    </h3> 
    <br/>
    <h4>
        <a class="btn btn-warning" id="buttton2"> Partial view 2</a>
    </h4>
   <br/> <br/>
    <div id="testsim">
    </div>




<script type="text/javascript">
$(function () {
    $('#button1').click(function () {
        $.get('@Url.Action("partialview1", "Home")', function (data1) {
            if (data1) {
                $('#testsim').replaceWith(data);
            }
        });
        });
         $('#button2').click(function () {
             $.get('@Url.Action("partialview2", "Home")', function (data2) {
                 if (data2) {
                     $('#testsim').replaceWith(data2);
                 }
        });
        });


});

</script>

I'm trying to achieve button clicks to toggle between two buttons, everytime button click should replace the content in div tag.我正在尝试实现按钮单击以在两个按钮之间切换,每次按钮单击都应替换 div 标签中的内容。 Any help would be appreciated.任何帮助,将不胜感激。

I think the problem is because of replaceWith() which replaces the element itself ie outerHTML -我认为问题是因为replaceWith()替换了元素本身,即outerHTML -

 $(function() { let $html, current; $('#button1').click(function() { /* $.get('@Url.Action("partialview1", "Home")', function(data1) { if (data1) { $('#testsim').replaceWith(data); } });*/ current = `button 1 was <em>clicked</em>`; $html = `<div><strong>${current}</strong></div>`; $('#testsim').replaceWith($html); }); $('#button2').click(function() { /*$.get('@Url.Action("partialview2", "Home")', function(data2) { if (data2) { $('#testsim').replaceWith(data2); } });*/ current = `button 2 was <strong>clicked</strong>`; $html = `<div><em>${current}</em></div>`; $('#testsim').replaceWith($html); }); });
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h3> <a class="btn btn-warning" id="button1"> Partial View 1</a> </h3> <br/> <h4> <a class="btn btn-warning" id="button2"> Partial view 2</a> </h4> <br/> <br/> <div id="testsim" style="background: aquamarine; height: 200px"> </div>

As you can see that the styling of the element disappears after replacing.如您所见,替换后元素的样式消失了。 If you want to perform this operation then you should use html() which replaces only innerHTML -如果要执行此操作,则应使用html()仅替换innerHTML -

 $(function() { let $html, current; $('#button1').click(function() { /* $.get('@Url.Action("partialview1", "Home")', function(data1) { if (data1) { $('#testsim').replaceWith(data); } });*/ current = `button 1 was <em>clicked</em>`; $html = `<div><strong>${current}</strong></div>`; $('#testsim').html($html); }); $('#button2').click(function() { /*$.get('@Url.Action("partialview2", "Home")', function(data2) { if (data2) { $('#testsim').replaceWith(data2); } });*/ current = `button 2 was <strong>clicked</strong>`; $html = `<div><em>${current}</em></div>`; $('#testsim').html($html); }); });
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h3> <a class="btn btn-warning" id="button1"> Partial View 1</a> </h3> <br/> <h4> <a class="btn btn-warning" id="button2"> Partial view 2</a> </h4> <br/> <br/> <div id="testsim" style="background: aquamarine; height: 200px"> </div>

Hope this helps you.希望这对您有所帮助。

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

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