如何仅在智能手机上编码Bootstrap手风琴

埃里亚·范·阿切特迪克

这段代码运作良好:

<!doctype html> <html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link href="css/style.css" rel="stylesheet">
</head>
<body> 

<div id="accordion" class="container"> 
<div class="card-deck">

<div class="card"> 
<div class="card-header" id="headingOne" > 
<h3 class="h3_toggle" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">Titel 1</h3> 
</div>
<div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordion"> 
<div class="card-body">Content 1</div> 
</div>
</div>

<div class="card"> <div class="card-header" id="headingTwo" > 
<h3 data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">Titel 2</h3> 
</div>
<div id="collapseTwo" class="collapse show" aria-labelledby="headingTwo" data-parent="#accordion"> 
<div class="card-body">Content 2</div> 
</div> 
</div> 
</div> 
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.4.1/jquery.easing.min.js"></script>
<script src="js/script.js"></script> </body> </html>

但是我只想在智能手机上使用Bootstrap的手风琴功能。因此,我从HTML标记中删除了所有手风琴属性,并将其添加到script.js中:

(function ($) {
  "use strict";

if (screenwidth <= 768) {
$('.h3_toggle').attr("data-toggle", "collapse").attr("data-target", "#collapseOne").attr("aria-expanded", "true").attr("aria-controls", "collapseOne");
$('#collapseOne').addClass("collapse show");
$('#collapseOne').attr("aria-labelledby", "headingOne").attr("data-parent", "#accordion");    }

})(jQuery); 

但这是行不通的。知道为什么吗?

阿卜杜拉赫曼

删除以下属性,然后根据宽度添加属性,如下所示。

您可以按如下方式使用调整大小功能:如果窗口调整大小,它将触发,并且首次启动时,您必须检查大小。

<!doctype html> 
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

</head>
<body> 

<div id="accordion" class="container"> 
<div class="card-deck">

	<div class="card"> 
		<div class="card-header" id="headingOne" > 
			<h3 class="h3_toggleOne">Titel 1</h3> 
		</div>
	<div id="collapseOne"> 
		<div class="card-body">Content 1</div> 
	</div>
	</div>

	<div class="card"> 
		<div class="card-header" id="headingTwo" > 
			<h3 class="h3_toggleTwo">Titel 2</h3> 
		</div>
	<div id="collapseTwo"> 
		<div class="card-body">Content 2</div> 
	</div> 
	</div> 
</div> 
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.4.1/jquery.easing.min.js"></script>
<script>

if (screen.width <= 768) { // this will work only one time at first
    addAttributes();
}else {
	removeAttributes();
}

$(window).resize(function() { // this will be work every time screen resize
  	if (screen.width <= 768) {
    	addAttributes();
	} else {
		removeAttributes();
    }
});

function addAttributes(){
	$('.h3_toggleOne').attr("data-toggle", "collapse").attr("data-target", "#collapseOne").attr("aria-expanded", "true").attr("aria-controls", "collapseOne");
    $('#collapseOne').addClass("collapse show");
    $('#collapseOne').attr("aria-labelledby", "headingOne").attr("data-parent", "#accordion");   
  
    $('.h3_toggleTwo').attr("data-toggle", "collapse").attr("data-target", "#collapseTwo").attr("aria-expanded", "false").attr("aria-controls", "collapseTwo");
    $('#collapseTwo').addClass("collapse");
    $('#collapseTwo').attr("aria-labelledby", "headingTwo").attr("data-parent", "#accordion");
}
function removeAttributes(){
	$('.h3_toggleOne').removeAttr("data-toggle").removeAttr("data-target").removeAttr("aria-expanded").removeAttr("aria-controls");
	$('#collapseOne').removeClass("collapse show");
	$('#collapseOne').removeAttr("aria-labelledby").removeAttr("data-parent"); 
	$('.h3_toggleTwo').removeAttr("data-toggle").removeAttr("data-target").removeAttr("aria-expanded").removeAttr("aria-controls");
	$('#collapseTwo').removeClass("collapse show");
	$('#collapseTwo').removeAttr("aria-labelledby").removeAttr("data-parent");
}
</script> 
</body> 
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Bootstrap布局在智能手机上过宽

来自分类Dev

Github仅在智能手机上页面CSS麻烦

来自分类Dev

OOP设计与智能手机上的性能

来自分类Dev

在智能手机上运行VBA

来自分类Dev

如何在智能手机上相邻显示两个div?

来自分类Dev

如何在物理Android智能手机上跟踪运行说明/分支机构?

来自分类Dev

如何获取智能手机上安装的杀毒软件信息?

来自分类Dev

如何对NFC标签进行编码以将智能手机与蓝牙设备配对

来自分类Dev

如何使我的网站响应智能手机

来自分类Dev

如何安装智能手机以恢复文件

来自分类Dev

Bootstrap手风琴

来自分类Dev

Bootstrap手风琴

来自分类Dev

FullPage JS:如何在智能手机上“禁用” FullPage并将部分显示为长页?

来自分类Dev

我可以在Android智能手机上调试Android Wear应用程序吗?(如何)

来自分类Dev

陀螺仪:如何在智能手机上将数据转换为世界坐标系?

来自分类Dev

android智能手机上的固定宽度html布局

来自分类Dev

防止在智能手机上滚动特定屏幕尺寸

来自分类Dev

除非已缓存,否则SVG图片不会在智能手机上加载

来自分类Dev

React网站按钮和输入在智能手机上不起作用

来自分类Dev

为什么程序在特定的智能手机上抛出OutOfMemoryError?

来自分类Dev

使用Three.js对球体进行纹理处理无法在智能手机上使用

来自分类Dev

我在智能手机上没有任何CSS

来自分类Dev

HTML和CSS布局在智能手机上无法正确显示

来自分类Dev

智能手机上的位置服务-它使用哪个物理网络?

来自分类Dev

在智能手机上安装两个操作系统

来自分类Dev

返回浏览器时,PHP会话在智能手机上丢失

来自分类Dev

智能手机上的媒体查询网站视图

来自分类Dev

android应用未出现在智能手机上

来自分类Dev

使用相机在Android智能手机上进行心率监测

Related 相关文章

  1. 1

    Bootstrap布局在智能手机上过宽

  2. 2

    Github仅在智能手机上页面CSS麻烦

  3. 3

    OOP设计与智能手机上的性能

  4. 4

    在智能手机上运行VBA

  5. 5

    如何在智能手机上相邻显示两个div?

  6. 6

    如何在物理Android智能手机上跟踪运行说明/分支机构?

  7. 7

    如何获取智能手机上安装的杀毒软件信息?

  8. 8

    如何对NFC标签进行编码以将智能手机与蓝牙设备配对

  9. 9

    如何使我的网站响应智能手机

  10. 10

    如何安装智能手机以恢复文件

  11. 11

    Bootstrap手风琴

  12. 12

    Bootstrap手风琴

  13. 13

    FullPage JS:如何在智能手机上“禁用” FullPage并将部分显示为长页?

  14. 14

    我可以在Android智能手机上调试Android Wear应用程序吗?(如何)

  15. 15

    陀螺仪:如何在智能手机上将数据转换为世界坐标系?

  16. 16

    android智能手机上的固定宽度html布局

  17. 17

    防止在智能手机上滚动特定屏幕尺寸

  18. 18

    除非已缓存,否则SVG图片不会在智能手机上加载

  19. 19

    React网站按钮和输入在智能手机上不起作用

  20. 20

    为什么程序在特定的智能手机上抛出OutOfMemoryError?

  21. 21

    使用Three.js对球体进行纹理处理无法在智能手机上使用

  22. 22

    我在智能手机上没有任何CSS

  23. 23

    HTML和CSS布局在智能手机上无法正确显示

  24. 24

    智能手机上的位置服务-它使用哪个物理网络?

  25. 25

    在智能手机上安装两个操作系统

  26. 26

    返回浏览器时,PHP会话在智能手机上丢失

  27. 27

    智能手机上的媒体查询网站视图

  28. 28

    android应用未出现在智能手机上

  29. 29

    使用相机在Android智能手机上进行心率监测

热门标签

归档