如何动态添加 jquery 手风琴到页面?

发布于 2024-11-17 11:30:36 字数 1963 浏览 1 评论 0原文

我有一个 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.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一花一树开 2024-11-24 11:30:36

首先你应该删除那个丑陋的 html('....blabla....'); 它太糟糕了......

把它放在你的普通 html 中,隐藏它,然后使用 clone()

jquery

 var html_data = $('#hidden_wrapper').clone().html();
    $('#detailTable').empty();
    $('<div>')
    .attr('id','healthCheckSpan')
    .html(html_data)       
    .appendTo('#detailTable').delay(1).queue(function(){
        $( "#hcAccordion" ).accordion();
         // now do not use ID as this would change if there are multiple ones... use classes .hcAccordion unless it is once.
    });

html

<div id="hidden_wrapper">... accordion goes here...</div>

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

 var html_data = $('#hidden_wrapper').clone().html();
    $('#detailTable').empty();
    $('<div>')
    .attr('id','healthCheckSpan')
    .html(html_data)       
    .appendTo('#detailTable').delay(1).queue(function(){
        $( "#hcAccordion" ).accordion();
         // now do not use ID as this would change if there are multiple ones... use classes .hcAccordion unless it is once.
    });

html

<div id="hidden_wrapper">... accordion goes here...</div>
智商已欠费 2024-11-24 11:30:36

我认为你应该创建你的手风琴,然后根据需要添加或删除 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.

缪败 2024-11-24 11:30:36

已添加以下代码,但 js 未执行,因为它应该在就绪时加载,我猜这已经引发了。

删除此代码

    '<script>$(document).ready(function(){'+
'$(function() {     $( "#hcAccordion" ).accordion();    });'+
'});</script>'+ 

并在附加代码后调用 $( "#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

    '<script>$(document).ready(function(){'+
'$(function() {     $( "#hcAccordion" ).accordion();    });'+
'});</script>'+ 

and call $( "#hcAccordion" ).accordion(); after ur append code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文