通过javascript切换html内容的最佳方式

时间:2012-11-14 08:12:14

标签: javascript jquery html css

请问您如何通过javascript切换html内容?

例如:

if($sid == 0)
 {one html code}
else
 {another html code 2}

LESS,jQuery,CSS3等 - 哪种方式更好?

3 个答案:

答案 0 :(得分:3)

您应该使用jQuery load并传回HTML内容,例如:

if($sid == 0) {
  $('#result').load('ajax/test.html', function() {
     alert('Load was performed. (sid equals 0');
  });
} else {
  $('#result').load('ajax/othercontent.html', function() {
     alert('Load was performed. (sid is not equal to 0');
  });
}

http://api.jquery.com/load/

答案 1 :(得分:0)

jQuery旨在以非浏览器特定的方式执行您所要求的(更明显地)。

<html>
<head>
<style>
  .hidden { display: none; }
</style>

<script type="text/javascript">
  $(function() {
    if(somecondition) $('#div1').show();
     else $('#div2').show();
  }); 
</script>
</head>

<body>
  <div id="div1" class="hidden">...</div>
  <div id="div2" class="hidden">...</div>
</body>
</html>

答案 2 :(得分:0)

我不确定是什么

if($sid == 0)

应该是,但如果你想改变html内容,你可以使用

$("#id").html("<span>your html</span>");