gpt4 book ai didi

javascript - 循环遍历内部 jquery Accordion 字段

转载 作者:行者123 更新时间:2023-11-28 02:33:50 24 4
gpt4 key购买 nike

有没有办法为 jquery Accordion 内的 and 语句创建循环,以便我可以迭代我的 JSON。

下面的代码可以使用 JSON,因此我希望 JSON 中的数据填充我的 jquery Accordion 。只是不知道如何解决 Accordion 所需的 h3 和 div 标签。

         <html>



<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<head>

<link rel="stylesheet" href="jquery-ui-1.8.23.custom/development-bundle/themes/base/jquery.ui.all.css">
<script src="jquery-ui-1.8.23.custom/development-bundle/jquery-1.8.0.js"></script>
<script src="jquery-ui-1.8.23.custom/development-bundle/ui/jquery.ui.core.js"></script>
<script src="jquery-ui-1.8.23.custom/development-bundle/ui/jquery.ui.widget.js"></script>
<script src="jquery-ui-1.8.23.custom/development-bundle/ui/jquery.ui.mouse.js"></script>
<script src="jquery-ui-1.8.23.custom/development-bundle/ui/jquery.ui.selectable.js"></script>
<script src="jquery-ui-1.8.23.custom/development-bundle/ui/jquery.ui.accordion.js"></script>
<link rel="stylesheet" href="jquery-ui-1.8.23.custom/development-bundle/demos/demos.css">
<script type='text/javascript' src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>

<script>
$(function() {
$( "#accordion" ).accordion();
});
</script><script>
$(document).ready(function(){
/* call the php that has the php array which is json_encoded */
$.getJSON(ReturnPlacesJSON.php, function(data) {
/* data will hold the php array as a javascript object */
$.each(data, function(key, val) {
var latlng = new google.maps.LatLng(val.xcoord, val.ycoord);
var title = (val.title)?val.title:""
var icon = 'http://shanewmiller.com/Specials/images/beermug.png';
var special = val.Description;
var end = (val.endtime)?val.endtime:""
var start = (val.starttime)?val.starttime+" - ":""
var day = (val.day)?val.day:""
var html = val.name + val.address + special + day + start + end;

$('<h3 />').html(special).appendTo('#accordion');
$('<div />').html(val.name + ' ' + val.address).appendTo('#accordion');
});
});
});

</script>


</head>
<body>

<div id="accordion">
</div>


</body></html>

最佳答案

您想要做的是在 Accordion div 内创建 h3 和 div$('#accordion > h3').each()$('#accordion > div').each() 所做的是迭代 h3 和 div在 Accordion 内部,没有。

您实际上需要做的是迭代 json - 通过调用 $.each(data, ...) 来完成 - 然后为每个项目创建一个新的 h3和一个包含该内容的新 div。您可以使用 jQuery 创建元素,如下所示:

$('<h3 />').html(special).appendTo('#accordion');
$('<div />').html(val.name + ' ' + val.address).appendTo('#accordion');

我相信其余的代码必须进行微调。例如,我不确定调用 accordion(),然后填充 div 是否能达到所需的效果。

关于javascript - 循环遍历内部 jquery Accordion 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13749905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com