Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过javascript切换html内容的最佳方法_Javascript_Jquery_Html_Css - Fatal编程技术网

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

通过javascript切换html内容的最佳方法,javascript,jquery,html,css,Javascript,Jquery,Html,Css,您能告诉我如何通过javascript切换html内容吗 例如: if($sid == 0) {one html code} else {another html code 2} if($sid == 0) { $('#result').load('ajax/test.html', function() { alert('Load was performed. (sid equals 0'); }); } else { $('#result').load('ajax/

您能告诉我如何通过javascript切换html内容吗

例如:

if($sid == 0)
 {one html code}
else
 {another html code 2}
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');
  });
}

更少、jQuery、CSS3等等——哪种方式更好?

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>

.hidden{显示:无;}
$(函数(){
if(somecondition)$('#div1').show();
else$('#div2').show();
}); 
...
...
我不确定是什么

if($sid == 0)
很好,但是如果您想更改html内容,可以使用

$("#id").html("<span>your html</span>");
$(“#id”).html(“您的html”);

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

if($sid == 0)
 {one html code}
else
 {another html code 2}
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');
  });
}

您还没有指定好的标准,或者您所说的切换html内容的意思,而伪代码也没有什么意义(看起来html将直接嵌入JavaScript——无法做到这一点)。