This article mainly talks about how to parse JSON through javascript and get the data and then add it to HTML. Friends in need can refer to it.
When writing pages with logical and repetitive content, using json data can significantly improve programming efficiency and facilitate later data maintenance. Therefore, on the video topic page, multiple columns of video data need to be displayed, and I chose to use json.
HTML is as follows (only the key parts are shown, JQ needs to be cited)
<p class="container-fluid content "> <p class="container neirong"> <p class="left fl"> <p class="title"> 热门视频 </p> <p class="medialist"> </p> </p></p> </p>
JS is as follows
<script> $(document).ready(function(){ console.log(1111) $.getJSON('data.json',function(data){ console.log(222) var mediahtml=""; $.each(data,function(i,data) { mediahtml+='<p class="media">'+ '<p class="media-left">'+ '<a data-toggle="modal" data-target="#myModal">'+ '<img class="media-object lazy" src="/static/imghwm/default1.png" data-src="'+data[" imgsrc"]+ '" alt="">'+ '</a>'+'</p>'+ '<p class="media-body">'+ '<p class="title">'+ '<span class="classify">'+ data["classify"]+ '</span>'+ '<span class="titlename media-heading">'+ data['titlename']+ '</span>'+ '</p>'+ '<span class="time">'+ '<span class="glyphicon glyphicon-time"></span> '+ '<span>'+data['pubdate']+'</span>'+ '<p>'+data["intro"]+'</p>'+ '<p class="guest">'+ '<span class="jia">嘉</span>'+ '<span class="name">'+data["name"]+'</span>'+ '<span class="position">'+data["position"]+'</span>'+ '<span class="glyphicon glyphicon-eye-open"></span>'+ '<span class="click-rite"></span>'+ '</p>'+ '</p>'+ '<p class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">'+ '<p class="modal-dialog" role="document">'+ '<p class="modal-content">'+ '<p class="modal-header">'+ '<button type="button" class="close" data-dismiss="modal" aria-label="Close">'+ '<span aria-hidden="true">×</span>'+ '</button>'+ '</p>'+ '<p class="modal-body"></p>'+ '</p>'+ '</p>'+ '</p>' // var url_mobi=data.url_mobi; // var url_pc=data.url_pc; // if ((navigator.userAgent.match(/(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symbian|Windows Phone|Phone)/i))) { // $('.modal-body').prepend(url_mobi); // }else{ // $('.modal-body').prepend(url_pc); // } // // }) $('.medialist').after(mediahtml); }) }) $('#myModal').on('shown.bs.modal', function (e) { // 关键代码,如没将modal设置为 block,则$modala_dialog.height() 为零 $(this).css('display', 'block'); var modalHeight = $(window).height() / 2 - $('#myModal .modal-dialog').height() / 2; $(this).find('.modal-dialog').css({ 'margin-top': modalHeight }); }); //点击预览图时判断 // $('.modal').on('click', function () { // if ($('#myModal').css("display") == "none") { // $('.modal-body').children('iframe').attr('src', ''); // } else { // $('.modal-body').children('iframe').attr('src', // 'https://v.qq.com/iframe/player.html?vid=v0508nqkm75&tiny=0&auto=0'); // } // }) </script>
You can ignore the comment part and it will not affect the content.
First, create a new json file. The problem that you need to pay attention to with json files is that json has requirements for data format and does not recognize various symbols in the url, so an error will be prompted. If it is not repaired, JS will be blocked. process, causing the data not to be displayed on the page. It took me a long time to find this problem, and the json problem will not report an error in js. The solution is to use the encode method to format the url, and then add it to json. You should also use decode to convert it back in html.
The second pitfall is to insert html into a certain tag. There are four methods. You can use after to achieve this. Don't use it the other way around.
The third point is that you need to pay attention. Don’t forget the plus sign when splicing strings. If one is missing, there will be problems. It will take a long time to find a small problem. Moreover, JS will not report an error when splicing errors. It is very difficult. Discover.
The above is how to get JSON data and load it.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
How to use javascript to implement QQ space photo album display
Related js event loop mechanism (detailed tutorial)
Summary on JS sorting algorithm
How to make a cone using JS canvas
The above is the detailed content of How to get JSON data through JS in html. For more information, please follow other related articles on the PHP Chinese website!

本篇文章带大家了解一下HTML(超文本标记语言),介绍一下HTML的本质,HTML文档的结构、HTML文档的基本标签和图像标签、列表、表格标签、媒体元素、表单,希望对大家有所帮助!

不算。html是一种用来告知浏览器如何组织页面的标记语言,而CSS是一种用来表现HTML或XML等文件样式的样式设计语言;html和css不具备很强的逻辑性和流程控制功能,缺乏灵活性,且html和css不能按照人类的设计对一件工作进行重复的循环,直至得到让人类满意的答案。

总结了一些web前端面试(笔试)题分享给大家,本篇文章就先给大家分享HTML部分的笔试题(附答案),大家可以自己做做,看看能答对几个!

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

在html中,document是文档对象的意思,代表浏览器窗口的文档;document对象是window对象的子对象,所以可通过“window.document”属性对其进行访问,每个载入浏览器的HTML文档都会成为Document对象。

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

3种取消方法:1、给td元素添加“border:none”无边框样式即可,语法“td{border:none}”。2、给td元素添加“border:0”样式,语法“td{border:0;}”,将td边框的宽度设置为0即可。3、给td元素添加“border:transparent”样式,语法“td{border:transparent;}”,将td边框的颜色设置为透明即可。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
