我有一个
<div id="content">
</div>
以及三个存储不同html的js变量:content1
、content2
和content3
。
通过用户交互,上述div的内容会更改为存储在js变量中的内容之一。
通过用户交互直接将 div
内容设置为我需要的内容更好:
$("#content").html(content2);
或将 div
结构更改为:
<div id="content">
<div id="c1">
// value of content1 variable here
</div>
<div id="c2">
// value of content2 variable here
</div>
<div id="c3">
// value of content3 variable here
</div>
</div>
并对内部 block 执行 hide()
和 show()
,即当我希望显示 content2 时:
$("#c1").hide();
$("#c2").show();
$("#c3").hide();
?
最佳答案
我会说隐藏和显示 div。
它的强度较低,如果 javascript 变量内的内容恰好包含您要绑定(bind)的元素,则不必每次刷新内容时都重新绑定(bind),并且如果您想在两者之间有某种动画不同的内容,多个 div 也允许。
顺便说一句,使用 jQuery 可以用更少的代码来完成类似的事情
$("#c2").show().siblings().hide();
关于javascript - jQuery hide()、show() 或 html(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26528146/