如何动态添加 jquery 手风琴到页面?
我有一个 div
,其中我附加不同的小部件,有时附加其他内容,所以为了让 jsp 页面看起来不麻烦,我我正在删除 detailTable
中的所有现有元素,并通过单击添加内容。现在我想添加一个 jQuery 手风琴,但它似乎不起作用。请提供这种情况下的解决方案。谢谢这是我正在做的事情,在按钮单击“我的屏幕截图”上的 detailTable
中删除和添加手风琴
$('#detailTable').empty();
$('<div>')
.attr('id','healthCheckSpan')
.html('<div class="titleBlue">Health Check Summary</div>'+
'<table style="border:#2F5882 1px solid;width:100%;" cellspacing="1" cellpadding="1">'+
'<thead>'+
'<tr style="color :#FFFFFF;background-color: #8EA4BB">'+
'<th width="10%" align="center"><b>Recommendations</b></th>'+
'</tr>'+
'</thead>'+
'<tbody >'+
'<tr style="color :#2F5882;background-color: #EDF1F5">'+
'<td align="left" width="10%">'+
'<span id="recommendations">'+
'<div id="hcAccordion">'+
'<h3><a href="#">Error</a></h3>'+
'<div><p id="errorhc">ERROR'+
'</p></div>'+
'<h3><a href="#">Warning</a></h3>'+
'<div><p id="warninghc">WARNING'+
'</p></div>'+
'<h3><a href="#">Info</a></h3>'+
'<div><p id="infohc">INFO'+
'</p></div>'+
'</div>'+
'<script>$(document).ready(function(){'+
'$(function() { $( "#hcAccordion" ).accordion(); });'+
'});</script>'+
'</span>'+
'</td>'+
'</tr>'+
'</tbody>'+
'</table>'+
'</div>')
.appendTo('#detailTable');
,这里我只是得到了一个应该是手风琴的东西,但根本没有任何效果。
I have a div <div id="detailTable" width="100%">
in which i append different widgets sometimes other content, so in order for the jsp page not to look cumbersome, i am removing any existing elements inside detailTable
and adding contents on some click. Now I want to add a jQuery accordion but it does not seem to work. Please provide a solution in this context. Thanks
Here is what i am doing to remove and add accordion in detailTable
on button click
$('#detailTable').empty();
$('<div>')
.attr('id','healthCheckSpan')
.html('<div class="titleBlue">Health Check Summary</div>'+
'<table style="border:#2F5882 1px solid;width:100%;" cellspacing="1" cellpadding="1">'+
'<thead>'+
'<tr style="color :#FFFFFF;background-color: #8EA4BB">'+
'<th width="10%" align="center"><b>Recommendations</b></th>'+
'</tr>'+
'</thead>'+
'<tbody >'+
'<tr style="color :#2F5882;background-color: #EDF1F5">'+
'<td align="left" width="10%">'+
'<span id="recommendations">'+
'<div id="hcAccordion">'+
'<h3><a href="#">Error</a></h3>'+
'<div><p id="errorhc">ERROR'+
'</p></div>'+
'<h3><a href="#">Warning</a></h3>'+
'<div><p id="warninghc">WARNING'+
'</p></div>'+
'<h3><a href="#">Info</a></h3>'+
'<div><p id="infohc">INFO'+
'</p></div>'+
'</div>'+
'<script>$(document).ready(function(){'+
'$(function() { $( "#hcAccordion" ).accordion(); });'+
'});</script>'+
'</span>'+
'</td>'+
'</tr>'+
'</tbody>'+
'</table>'+
'</div>')
.appendTo('#detailTable');
My screenshot, here i just get a supposed to be accordion but no effects at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先你应该删除那个丑陋的
html('....blabla....');
它太糟糕了......把它放在你的普通 html 中,隐藏它,然后使用
clone()
、jquery
html
firstly you should remove that ugly
html('....blabla....');
it's terrible...put that in your normal html, and hide it, then copy it using
clone()
,jquery
html
我认为你应该创建你的手风琴,然后根据需要添加或删除 div。所以采取
'$(文档).ready(函数(){'+
'$(function() { $( "#hcAccordion" ).accordion(); });'+
'});'
从动态 HTML 中取出并让它在页面加载时运行。
I think you should create your accordion and then just append or delete divs as you need to. So take
'$(document).ready(function(){'+
'$(function() { $( "#hcAccordion" ).accordion(); });'+
'});'
out of your dynamic HTML and have it run as the page loads.
已添加以下代码,但 js 未执行,因为它应该在就绪时加载,我猜这已经引发了。
删除此代码
并在附加代码后调用
$( "#hcAccordion" ).accordion();
Following code already added but js is not excuted as it is supposed to load on ready, which is i guess already raised.
Get rid of this code
and call
$( "#hcAccordion" ).accordion();
after ur append code