单击按钮动态加载 DIV 的最简单方法?
我在这里有一个相对简单的问题。
我要求的是这个;我需要一个页面,顶部有 5 个按钮,下面有一个 DIV(最初是隐藏的)。单击按钮时,会将内容加载到 DIV 中(以另一个 DIV 的形式)
例如。
[Button 1] [Button 2] [Button 3] [Button 4] [Button 5] //5 buttons or links
<div id="content">
// area for content to be loaded
</div>
<div id="content1">
//could be a table or image
</div>
<div id="content2">
//could be a table or image
</div>
<div id="content3">
//could be a table or image
</div>
<div id="content4">
//could be a table or image
</div>
<div id="content5">
//could be a table or image
</div>
在上面的示例中,当用户加载页面时,他们会看到 5 个按钮。如果他们按下按钮 5,则会将“content5”DIV 加载到“content”DIV 内,例如。
<div id="content">
<div id="content5">
</div>
</div>
如果选择另一个按钮,它将加载其内容。
可以用 jQuery 或简单的 JavaScript 来实现。
非常感谢
I have a relatively simple question here.
What I require is this; I need to have a page with 5 buttons at the top and a DIV beneath them (initially hidden). When a button is clicked, it loads content into the DIV (in the form of another DIV)
eg.
[Button 1] [Button 2] [Button 3] [Button 4] [Button 5] //5 buttons or links
<div id="content">
// area for content to be loaded
</div>
<div id="content1">
//could be a table or image
</div>
<div id="content2">
//could be a table or image
</div>
<div id="content3">
//could be a table or image
</div>
<div id="content4">
//could be a table or image
</div>
<div id="content5">
//could be a table or image
</div>
In the above example, when the user loads the page they see 5 buttons. If they press button 5, it loads the "content5" DIV inside of the "content" DIV eg.
<div id="content">
<div id="content5">
</div>
</div>
If another button is selected it loads it's content.
Can be achieved with jQuery or simple JavaScript.
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在所有按钮上绑定一个单击处理程序。在这个特定示例中,明智的方法是使用元素的索引来确定哪个
div
属于button
。演示:http://www.jsfiddle.net/8f8jt/
这个将为所有
You need to bind a click handler on all of the buttons. Smart way in this particular example would be, to use the index of the elements to determine which
div
belongs to abutton
.Demo: http://www.jsfiddle.net/8f8jt/
This will add an click event handler to all
<button>
nodes. On click, it looks for the index of the current clicked button (.index()
help) and uses this value to query for the accordant<div>
elements id. Once found, use.html()
help to get and set the value.您还可以使用 jQueryUI 选项卡,但它需要额外的脚本。
You also can use jQueryUI tabs, but it requires additional script.
尝试
此方法会清除所有现有内容并将正确的 div 复制到主 #content div 中。
Try this
This way clears any existing content and copies the correct div into the main #content div.