簡體   English   中英

jquery ReplaceWith 不使用 2 個按鈕

[英]jquery ReplaceWith not working with 2 buttons

我創建了 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>

我正在嘗試實現按鈕單擊以在兩個按鈕之間切換,每次按鈕單擊都應替換 div 標簽中的內容。 任何幫助,將不勝感激。

我認為問題是因為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>

如您所見,替換后元素的樣式消失了。 如果要執行此操作,則應使用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>

希望這對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM