jquery ReplaceWith 不适用于 2 个按钮

麦迪

我创建了 2 个按钮,每个按钮将根据部分视图替换内容,当我单击按钮时,我可以在页面上加载部分视图,但这只能工作一次,例如我单击了 button-1 并加载了数据,现在如果我单击按钮 2 不起作用,我需要返回主页再次单击按钮 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 标签中的内容。任何帮助,将不胜感激。

sauhardnc

我认为问题是因为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()使用which 替换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>

希望这对你有帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章