我使用Bootstrap 4和jquery 3.2.1的html是由另一个脚本动态生成的,由几个元素组成,其基本单元如下所示:
<div class="card" style="width: 95%;">
<div class="card-body">
<h5 class="card-title"><div class="alert alert-secondary" role="alert"><h4>Question 24 of 2947</h4></div></h5>
<h5 class="card-header"><p>A 72-year-old lady was treated non-operatively following a closed fracture of the radial shaft. Which of the following late complications would you least likely expect to see?Select one answer only</p></h5>
<p class="card-text"></p><ul><li>(A) Algodystrophy</li><li>(B) Infection</li><li>(C) Non-union</li><li>(D) Tendon rupture</li><li>(E) Volkmann’s ischaemic contracture</li></ul><p></p>
</div>
</div>
<button class="btn btn-primary columbrium" type="button" data-toggle="collapse" data-target="#yyGOkFXx" aria-expanded="true" aria-controls="collapseExample">
Answer
</button>
<button class="btn btn-primary" type="button" id="ExpandAllBtnyyGOkFXx">Expand All</button>
<script>
$( "button[id^='ExpandAllBtn']" ).bind('click', function() {
$('.collapse').collapse('toggle');
if ($( ".collapse" ).first().hasClass( "show" )) {
$( "button[id^='ExpandAllBtn']" ).html('Collapse All');
} else {
$( "button[id^='ExpandAllBtn']" ).html('Expand All');
}
});
</script>
<div class="collapse show" id="yyGOkFXx" style="">
<div class="card" style="width: 95%;">
<div class="card-body">
<p class="card-text">
</p><h2>Explanation
</h2>
<div id="ctl00">
<p><strong>My text</strong></p>
</div><p></p>
</div>
</div></div>
随附的javascript应该扩展可折叠的引导元素,并且一旦发生单击,也会更改按钮文本的名称。 尽管代码在控制台中运行时仍然有效,但单击按钮时它不会更改按钮的名称,尽管元素会按预期展开和折叠。我能错过什么?
$( "button[id^='ExpandAllBtn']" ).bind('click', function() {
$('.collapse').collapse('toggle');
if ($( ".collapse" ).first().hasClass( "show" )) {
$( "button[id^='ExpandAllBtn']" ).html('Collapse All');
} else {
$( "button[id^='ExpandAllBtn']" ).html('Expand All');
}
});
<html>
<head>
<title>Collection 2018</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</head>
<body>
<div class="card" style="width: 95%;">
<div class="card-body">
<h5 class="card-title"><div class="alert alert-secondary" role="alert"><h4>Question 24 of 2947</h4></div></h5>
<h5 class="card-header"><p>A 72-year-old lady was treated non-operatively following a closed fracture of the radial shaft. Which of the following late complications would you least likely expect to see?Select one answer only</p></h5>
<p class="card-text"></p><ul><li>(A) Algodystrophy</li><li>(B) Infection</li><li>(C) Non-union</li><li>(D) Tendon rupture</li><li>(E) Volkmann’s ischaemic contracture</li></ul><p></p>
</div>
</div>
<button class="btn btn-primary columbrium" type="button" data-toggle="collapse" data-target="#yyGOkFXx" aria-expanded="true" aria-controls="collapseExample">
Answer
</button>
<button class="btn btn-primary" type="button" id="ExpandAllBtnyyGOkFXx">Expand All</button>
<div class="collapse show" id="yyGOkFXx" style="">
<div class="card" style="width: 95%;">
<div class="card-body">
<p class="card-text">
</p><h2>Explanation
</h2>
<div id="ctl00">
<p><strong>My text</strong></p>
</div><p></p>
</div>
</div></div>
</body>
</html>
我的小提琴:https://jsfiddle.net/7rupvont/
要清楚,我的工作示例确实可以扩展和折叠元素。 我无法更改显示的html中匹配元素的innerhtml,但能够在控制台窗口中执行此操作。 没有延迟加载元素,这从代码片段/ jsfiddle。
答案 0 :(得分:2)
原因是所有Bootstrap 4方法都是异步的,因此当您调用.collapse()
时,它只是启动转换,但会立即返回到脚本的其余部分。在Collapse组件的Methods部分下描述了这种现象。
如果您在脚本中console.log
$('.collapse').first().hasClass('show')
的值,则会看到它始终等于false
。可折叠元素在即将显示时不会立即收到.show
类,但在即将隐藏时会立即丢失。
此问题的解决方案是使用折叠组件在切换时发出的Events。下面你会找到一个代码片段。
// jQuery `.bind()` has been deprecated, use `.on()` instead
$("button[id^='ExpandAllBtn']").on('click', function() {
$('.collapse').collapse('toggle');
// $('.collapse').first().hasClass('show') is always false here
console.log($('.collapse').first().hasClass('show'));
});
$('.collapse').first().on('shown.bs.collapse', function () {
$("button[id^='ExpandAllBtn']").html('Collapse All');
});
$('.collapse').first().on('hidden.bs.collapse', function () {
$("button[id^='ExpandAllBtn']").html('Expand All');
});
<div class="card" style="width: 95%;">
<div class="card-body">
<h5 class="card-title"><div class="alert alert-secondary" role="alert"><h4>Question 24 of 2947</h4></div></h5>
<h5 class="card-header"><p>A 72-year-old lady was treated non-operatively following a closed fracture of the radial shaft. Which of the following late complications would you least likely expect to see?Select one answer only</p></h5>
<p class="card-text"></p><ul><li>(A) Algodystrophy</li><li>(B) Infection</li><li>(C) Non-union</li><li>(D) Tendon rupture</li><li>(E) Volkmann’s ischaemic contracture</li></ul><p></p>
</div>
</div>
<button class="btn btn-primary columbrium" type="button" data-toggle="collapse" data-target="#yyGOkFXx" aria-expanded="true" aria-controls="collapseExample">Answer</button>
<button class="btn btn-primary" type="button" id="ExpandAllBtnyyGOkFXx">Expand All</button>
<div class="collapse" id="yyGOkFXx" style="">
<div class="card" style="width: 95%;">
<div class="card-body">
<h2>Explanation</h2>
<div id="ctl00">
<p><strong>My text</strong></p>
</div>
</div>
</div>
</div>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
注意:从jQuery 3.0开始,{@ 3}}方法已被弃用,您应该使用.bind()代替。
答案 1 :(得分:0)
好吧,由于Bootstraps崩溃的异步性,这不起作用。为了实际显示对dferencs的不同答案,我已经将我改成了一个不需要那么多事件监听器的解决方法:
$("button[id^='ExpandAllBtn']").on("click", function() {
$(".collapse").collapse("toggle");
if (($("button[id^='ExpandAllBtn']" ).html() === "Collapse All") && !$(".collapse").first().hasClass("show")) {
$("button[id^='ExpandAllBtn']" ).html("Expand All");
} else {
$("button[id^='ExpandAllBtn']" ).html("Collapse All");
}
});