如何使用jQuery更改可折叠内容的title属性
How do I change the title attribute on collapsible content using jQuery
我有这些可折叠的面板,我想让title属性来说明它们何时打开或关闭。我正在努力让它与我现在使用的脚本配合使用,但没有任何运气。
thx
<div class="container">
<h1>Information</h1>
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading" data-target="#content1">
<h2 class="panel-title" title="hidden content closed">
Milky Way Black Hole Belches
</h2>
</div>
<div id="content1" class="collapse">
<div class="panel-body">
The monster back hole at the center of the Milky Way belched out an exceptionally high number of powerful X-ray flares in August 2014, did the beast chow down on a passing gas cloud, or is this typical for black holes? </span>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading" data-target="#content2">
<h2 class="panel-title" title="hidden content Closed" >
Tiny Pluto Moon Kerberos Unveiled
</h2>
</div>
<div id="content2" class="collapse">
<div class="panel-body">
Newly received photos captured by NASA's New Horizons spacecraft reveal that Pluto's tiny moon Kerberos is smaller and brighter than researchers had expected.
</div>
</div>
</div>
</div>
</div>
<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();
});
http://jsfiddle.net/rzseLj27/3/
您的代码当前不起作用,因为您正在将标题更改应用于包含的#accordion
元素,而不是实际的标题。此外,你每次都会在手风琴上使用一个新的听众。
使用Bootstrap实现这一点的最佳方法是使用data-*
属性,然后侦听更改以更改标题。下面的代码正是您想要的;
工作小提琴
Javascript
$(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
<div class="container">
<h1>Information</h1>
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading" data-target="#content1" data-parent="#accordion" data-toggle="collapse">
<h2 class="panel-title" title="hidden content closed">
Milky Way Black Hole Belches
</h2>
</div>
<div id="content1" class="collapse">
<div class="panel-body">
The monster back hole at the center of the Milky Way belched out an exceptionally high number of powerful X-ray flares in August 2014, did the beast chow down on a passing gas cloud, or is this typical for black holes? </span>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading" data-target="#content2" data-parent="#accordion" data-toggle="collapse">
<h2 class="panel-title" title="hidden content Closed" >
Tiny Pluto Moon Kerberos Unveiled
</h2>
</div>
<div id="content2" class="collapse">
<div class="panel-body">
Newly received photos captured by NASA's New Horizons spacecraft reveal that Pluto's tiny moon Kerberos is smaller and brighter than researchers had expected.
</div>
</div>
</div>
</div>
</div>
更新(标题中包含文本)
如果您还想在元素标题中包含标题的文本,只需在侦听器中获取该文本并将其附加在上即可
工作小提琴
$(".collapse").on('hide.bs.collapse', function(){
var title = $(this).parents('div.panel').find('.panel-title')[0];
var titleText = $(title).text();
$(title).attr('title',titleText+' hidden content closed');
});
$(".collapse").on('show.bs.collapse', function(){
var title = $(this).parents('div.panel').find('.panel-title')[0];
var titleText = $(title).text();
$(title).attr('title',titleText+' hidden content open');
});
这会在面板标题div上设置标题,而不是h2,但会按照您的要求执行:http://jsfiddle.net/x057wp3L/2/
<div class="container">
<h1>Information</h1>
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading" data-target="#content1" title="hidden content closed">
<h2 class="panel-title">
Milky Way Black Hole Belches
</h2>
</div>
<div id="content1" class="collapse">
<div class="panel-body">
The monster back hole at the center of the Milky Way belched out an exceptionally high number of powerful X-ray flares in August 2014, did the beast chow down on a passing gas cloud, or is this typical for black holes? </span>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading" data-target="#content2" title="hidden content closed">
<h2 class="panel-title">
Tiny Pluto Moon Kerberos Unveiled
</h2>
</div>
<div id="content2" class="collapse">
<div class="panel-body">
Newly received photos captured by NASA's New Horizons spacecraft reveal that Pluto's tiny moon Kerberos is smaller and brighter than researchers had expected.
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('.panel-heading').click(function(){
var target = $(this).data("target");
$('#accordion').on('show.bs.collapse', function () {
$('#accordion .in').collapse('hide');
// Set the title of all panels to be "closed"
$('#accordion .panel-heading').each(function () { $(this).attr('title', 'hidden content closed'); });
});
$(target).collapse('toggle');
// Set the title of the clicked panel to be "open"
$(this).attr('title', 'hidden content open');
});
});
</script>
相关文章:
- 如何获取元素's的title属性
- 仅将CSS类添加到那些没有“title”属性的链接中
- 在D3.js中为svg:g元素添加title属性
- 在Oracle Apex 5.0中,无法在侧面导航列表模板中使用title属性
- 工具提示JavaScript在未给出“title”属性时会中断以下表单元素
- 在 JS 的单选按钮元素 (HTML) 中使用 title 属性
- Jquery,实时“title”属性更改
- jQuery:删除浏览器生成的 title 属性
- AngularJS “{{}}” insite title 属性不起作用
- jQuery/JavaScript 查询关于 title 属性
- 如何使用$(this)来动态检索锚点标记的title属性
- 修复了将CSS应用于title属性的脚本的错误
- 如何从tinymce文本区域中删除title属性
- 使用AngularJS将元素文本复制到title属性的干净方法
- jQuery:暂时删除HOVER上的元素TITLE属性
- 在Fancybox下面添加描述文本,而不是使用title属性
- 如何使用xml获取在其中设置的选项标记的title属性
- 如何使用jQuery更改可折叠内容的title属性
- 用JavaScript设置html按钮的title属性
- 如果元素的父元素带有title属性,则在某些浏览器中不会触发事件处理程序