通过javascript切换html内容的最佳方法
您能告诉我如何通过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/
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”);
您应该使用jQueryload
并将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——无法做到这一点)。