Javascript 如何使用jQuery更改可折叠内容的title属性

Javascript 如何使用jQuery更改可折叠内容的title属性,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,我有这些可折叠的面板,我想让title属性显示它们是打开还是关闭的。我试图让它与我现在使用的脚本一起工作,但没有任何运气 <script> $(document).ready(function() { $('.panel-heading').click(function(){ var target = $(this).data("target"); $('#accordion').on('show.bs.coll

我有这些可折叠的面板,我想让title属性显示它们是打开还是关闭的。我试图让它与我现在使用的脚本一起工作,但没有任何运气

<script>
    $(document).ready(function() {
        $('.panel-heading').click(function(){
            var target = $(this).data("target");
            $('#accordion').on('show.bs.collapse', function () {
                $('#accordion .in').collapse('hide')
                $(this).attr('title', 'hidden content closed');
            });
            $(target).collapse('toggle')
            $(this).attr('title', 'hidden content open');
        });             
    });
</script
thx

<script>
    $(document).ready(function() {
        $('.panel-heading').click(function(){
            var target = $(this).data("target");
            $('#accordion').on('show.bs.collapse', function () {
                $('#accordion .in').collapse('hide')
                $(this).attr('title', 'hidden content closed');
            });
            $(target).collapse('toggle')
            $(this).attr('title', 'hidden content open');
        });             
    });
</script

问询处
银河系黑洞喷发
2014年8月,位于银河系中心的怪兽背洞喷出了数量异常多的强大X射线耀斑,这是怪兽在经过的气体云上吞下的还是黑洞的典型现象?
小冥王星卫星
美国宇航局“新地平线”号宇宙飞船拍摄的最新照片显示,冥王星的小卫星比研究人员预期的更小、更亮。

<script>
    $(document).ready(function() {
        $('.panel-heading').click(function(){
            var target = $(this).data("target");
            $('#accordion').on('show.bs.collapse', function () {
                $('#accordion .in').collapse('hide')
                $(this).attr('title', 'hidden content closed');
            });
            $(target).collapse('toggle')
            $(this).attr('title', 'hidden content open');
        });             
    });
</script

$(文档).ready(函数(){
$('.panel heading')。单击(函数(){
var target=$(this).data(“target”);
$('#accordion').on('show.bs.collapse',function(){
$('#accordion.in')。折叠('hide'))
$(this.attr('title','hidden content closed');
});
$(目标)。折叠('切换')
$(this.attr('title','hidden content open');
});             
});

这将在面板标题div(而不是h2)上设置标题,但按照您的要求执行:

<script>
    $(document).ready(function() {
        $('.panel-heading').click(function(){
            var target = $(this).data("target");
            $('#accordion').on('show.bs.collapse', function () {
                $('#accordion .in').collapse('hide')
                $(this).attr('title', 'hidden content closed');
            });
            $(target).collapse('toggle')
            $(this).attr('title', 'hidden content open');
        });             
    });
</script

问询处
银河系黑洞喷发
2014年8月,位于银河系中心的怪兽背洞喷出了数量异常多的强大X射线耀斑,这是怪兽在经过的气体云上吞下的还是黑洞的典型现象?
小冥王星卫星
美国宇航局“新地平线”号宇宙飞船拍摄的最新照片显示,冥王星的小卫星比研究人员预期的更小、更亮。
$(文档).ready(函数(){
$('.panel heading')。单击(函数(){
var target=$(this).data(“target”);
$('#accordion').on('show.bs.collapse',function(){
$('#accordion.in')。折叠('hide');
//将所有面板的标题设置为“关闭”
$('#accordion.panel heading').each(函数(){$(this.attr('title','hidden content closed');});
});
$(目标)。折叠(“切换”);
//将单击面板的标题设置为“打开”
$(this.attr('title','hidden content open');
});             
});

我想这就是您需要的:

<script>
    $(document).ready(function() {
        $('.panel-heading').click(function(){
            var target = $(this).data("target");
            $('#accordion').on('show.bs.collapse', function () {
                $('#accordion .in').collapse('hide')
                $(this).attr('title', 'hidden content closed');
            });
            $(target).collapse('toggle')
            $(this).attr('title', 'hidden content open');
        });             
    });
</script
$('.panel-heading').click(function(){
            var target = $(this).attr("data-target");
            $(this).find("h2").toggleClass("inactive active");   
            $(this).find("h2").attr("title",$(this).find("h2").hasClass('inactive') ? 'hidden content closed' : 'hidden content opened');   
            $(target).toggle();
        }); 

您的代码当前不起作用,因为您正在将标题更改应用于包含的
#accordion
元素,而不是实际的标题。此外,您每次都要为该手风琴应用一个新的侦听器

<script>
    $(document).ready(function() {
        $('.panel-heading').click(function(){
            var target = $(this).data("target");
            $('#accordion').on('show.bs.collapse', function () {
                $('#accordion .in').collapse('hide')
                $(this).attr('title', 'hidden content closed');
            });
            $(target).collapse('toggle')
            $(this).attr('title', 'hidden content open');
        });             
    });
</script
使用Bootstrap实现这一点的最佳方法是使用
data-*
属性,然后监听更改以更改标题。下面的代码正是您想要的

<script>
    $(document).ready(function() {
        $('.panel-heading').click(function(){
            var target = $(this).data("target");
            $('#accordion').on('show.bs.collapse', function () {
                $('#accordion .in').collapse('hide')
                $(this).attr('title', 'hidden content closed');
            });
            $(target).collapse('toggle')
            $(this).attr('title', 'hidden content open');
        });             
    });
</script

<script>
    $(document).ready(function() {
        $('.panel-heading').click(function(){
            var target = $(this).data("target");
            $('#accordion').on('show.bs.collapse', function () {
                $('#accordion .in').collapse('hide')
                $(this).attr('title', 'hidden content closed');
            });
            $(target).collapse('toggle')
            $(this).attr('title', 'hidden content open');
        });             
    });
</script
Javascript

<script>
    $(document).ready(function() {
        $('.panel-heading').click(function(){
            var target = $(this).data("target");
            $('#accordion').on('show.bs.collapse', function () {
                $('#accordion .in').collapse('hide')
                $(this).attr('title', 'hidden content closed');
            });
            $(target).collapse('toggle')
            $(this).attr('title', 'hidden content open');
        });             
    });
</script
$(document).ready(function() {
    $(".collapse").on('hide.bs.collapse', function(){
        var title = $(this).parents('div.panel').find('.panel-title')[0];
        $(title).attr('title','hidden content closed');
    });

    $(".collapse").on('show.bs.collapse', function(){
       var title = $(this).parents('div.panel').find('.panel-title')[0];
       $(title).attr('title','hidden content open');
    });
});
HTML

<script>
    $(document).ready(function() {
        $('.panel-heading').click(function(){
            var target = $(this).data("target");
            $('#accordion').on('show.bs.collapse', function () {
                $('#accordion .in').collapse('hide')
                $(this).attr('title', 'hidden content closed');
            });
            $(target).collapse('toggle')
            $(this).attr('title', 'hidden content open');
        });             
    });
</script

谢谢,你能告诉我如何使用H2文本并将其与“隐藏内容打开”相结合吗。我一直在寻找一些例子,比如(文本)+隐藏内容,你能澄清一下吗?您想将
h2
的实际文本与生成的标题结合起来吗?因此,当您打开第一节时,
h2
的标题属性将更改为
银河系黑洞打嗝隐藏内容打开
,当它关闭时,它将变成
银河系黑洞打嗝隐藏内容关闭
?是的,这就是我正在尝试的。谢谢您的回复,我看着这个,试图更好地理解它。