许多来自JSON的hrefs,在切换时单击会显示diffrent id
Many a hrefs from JSON, on toggle click make diffrent id appear
我有一个JSON文件,里面有影院里的电影。当我点击电影标题时,我想让信息显示在正确的电影上。我的代码不起作用,但我可以让所有的电影都在点击时出现,无论是什么电影。
我希望能够点击,比方说,"Ender的游戏",然后让infodiv只显示关于那部电影的信息。我的代码在下面:
JavaScript:
$(document).ready(function() {
$.getJSON('json/store.json', function(results) {
//Get movie title
for (var i = 0; i < results['results'].length; i++) {
var title = '<p><a href="#">' + results['results'][i].title + '</a></p>';
$('#movies').append(title);
}
for (var i = 0; i < results['results'].length; i++) {
var title = $('#info').append("<p class='biohus'>" + results['results'][i].title + "</p>");
var img = $('#info').append("<img id='myndbio' width='200' height='300' class='img' src =" + results['results'][i].image + ">");
}
$('.title').click(function(){
$(this).parent().find('#info').slideToggle("slow");
});
$('.title').click(function(){
var dataMovieId = $(this).attr('data-movie-id');
$('#'+dataMovieId).slideToggle("slow");
});
});
});
Html:
<div id="bio" class = "content">
<center><h1 class="cinemaheadline">Myndir í bíó</h1></center>
<?php include 'php/curl.php';?>
<div id="movies" class="movies">
<!-- Movies -->
</div>
<div id="info" class="info">
<!-- info -->
</div>
</div>
和php连接到api:
<?php
//check if you have curl loaded
if(!function_exists("curl_init")) die("cURL extension is not installed");
//url
$url = 'http://apis.is/cinema';
$storejson = 'json/store.json';
//skrifa
$fw = fopen($storejson, 'w');
//start curl
$ch=curl_init($url);
//set options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute
$r=curl_exec($ch);
//close curl
curl_close($ch);
$arr = json_decode($r,true);
fwrite($fw, json_encode($arr, TRUE));
?>
这里的主要问题是您需要某种方式将标题与信息链接起来。可以将每个电影的标题和信息封装在自己的div中,也可以使用一些属性(数据电影)并为其提供一个名称或数字,以便将标题和信息关联起来。
如果第一种方法是你需要做一些类似的事情
$('.title').click(function(){
$(this).parent().find('.info').slideToggle("slow");
});
第二种方式是类似的;
$('.title').click(function(){
var dataMovieId = $(this).attr('data-movie-id');
$('#'+dataMovieId).slideToggle("slow");
});
最后,你不应该像现在这样使用id。例如,您将输出具有相同id的img标记。您应该在一个页面上只使用一次id。
编辑:完整代码;
$(document).ready(function() {
$.getJSON('json/store.json', function(results) {
for (var i = 0; i < results['results'].length; i++) {
// title link. note the data-id attr which is i, which we will put into the movie info, to tie them together
var title = '<p><a href="#" data-id="'+i+'">' + results['results'][i].title + '</a></p>';
// info section
var infoTitle = "<p class='biohus'>" + results['results'][i].title + "</p>";
var infoImg = "<img id='myndbio' width='200' height='300' class='img' src =" + results['results'][i].image + ">";
// wrap title and image in a div with the data-id attr and i
var info = '<div class="movie-info data-id="'+i+'">'+infoTitle+infoImg+'</div>';
// place both title and info sections in the right spots
$('#movies').append(title);
$('#info').append(info);
}
});
// set up click handlers
$('#movies a').click(function(){
// find data id of clicked movie
var id = $(this).attr('data-id');
// find movie info with correct id and show
$('#info .movie-info[data-id="'+id+'"]').slideToggle("slow");
});
});
相关文章:
- 角度的项目列表 ng 重复,ng 单击显示全宽描述
- Jquery on单击“显示信息”
- VBA IE单击/显示没有id或标记而不是超链接的javascript
- Asp.net 客户端单击显示表中其他单元格的值
- 通过键盘单击显示按钮上的按键事件
- AngularJS嵌套具有多维数组的元素,并通过单击显示它们
- 如何通过单击显示数组和按钮
- 单击显示随机图像(Javascript)
- 隐藏李的子项并通过单击显示
- Angularjs 通过 ng 单击显示更多省略号文本
- on单击显示/隐藏 想要在隐藏 javascript 上折叠图像大小
- 隐藏对象并通过单击显示它
- 单击显示在同一页面或同一选项卡中
- on单击“显示iframe 5000”,然后隐藏
- 在表单字段HTML/JQuery中单击显示图像
- 显示或隐藏通过单击显示的信息
- 单击“显示日期”字段,然后单击“选择日期”隐藏日期字段
- 当用户单击“显示链接”时,显示密码,再次单击时将其隐藏
- 单击显示更多时显示更多图像
- 如何从目录加载图像,然后单击显示