当前位置:网站首页>js的相关知识
js的相关知识
2022-07-24 00:46:00 【华为云】
1.$(function() {})
$(function() {}) 是jquery里面的一个写法,类似于原生js中的DOMContentLoaded事件。他其实就是$(document).ready(function()的简写。
那么这个函数是在什么时候执行的呢?
他是在DOM加载完毕之后就被执行了的,即页面所有的html标签(包括图片等)都加载完成了,浏览器已经响应完了,所有dom树全部展现到浏览器界面上了(就是各种各样的元素标签),一切准备就绪(其中图片,音视频等这些静态资源等还没加载前)这个函数就开始执行了。
2.JSON.stringify()
JSON.stringify() 方法用于将 JavaScript 值转换为 JSON 字符串。他的语法是JSON.stringify(value[, replacer[, space]]),其中value为需要转化的数据;replacer是可选项。用于转换结果的函数或数组。如果replacer为函数,则JSON.stringify将调用该函数,并传入每个成员的键和值。使用返回值而不是原始值。如果此函数返回 undefined,则排除成员。根对象的键是一个空字符串:""。如果replacer是一个数组,则仅转换该数组中具有键值的成员。成员的转换顺序与键在数组中的顺序一样;space也是一个可选项,是为文本添加缩进、空格和换行符的,如果 space 是一个数字,则返回值文本在每个级别缩进指定数目的空格,如果 space 大于 10,则文本缩进 10 个空格。space 也可以使用非数字,如:\t。
一般我们使用的JSON.stringify(value)来快速将数据转换为json字符串。
3.JSON.parse()
JSON.parse() 方法将数据转换为 JavaScript 对象。他的语法是JSON.parse(text[, reviver]),其中text是必选项, 他必须是一个有效的JSON字符串,如果不是会报错的;reviver是可选项,是一个转换结果的函数, 会为要转换的对象的每个成员调用这个函数。
4.JS获取当前网页的相关信息
在WEB开发中,时常会用到javascript来获取当前页面的url网址信息,下面是获取各个部分的小栗子
下面我们举个栗子URL= http://test.com/testPath?parm=123
4.1 window.location.href获取整个URL为字符串
var tem= window.location.href;返回:http://test.com/testPath?parm=123
4.2 window.location.protocol获取 URL 的协议部分
var tem= window.location.protocol; 返回:http:
4.3 window.location.host获取URL的主机部分
var tem= window.location.host; 返回:test.com
4.4 window.location.port获取与URL关联的端口号码
var tem= window.location.port; 返回:空字符(这是因为采用默认的80端口(即使手动添加了:80),那么返回值并不是默认的80而是空字符)
4.5 window.location.pathname获取与URL的路径部分
var tem= window.location.pathname;返回:/testPath
4.6 window.location.search获取 href 属性中跟在问号后面的部分
var tem= window.location.search; 返回:?parm=123
4.7 window.location.hash获取href属性中在"#"后面的参数
var tem= window.location.hash;返回:空字符
边栏推荐
- Accelerating matrix vector multiplication of special matrices with FFT
- How to improve data quality
- AWS Part 4 one machine and one secret
- Classic examples of C language - adding two scores
- Problem note - unable to open include file: "direct.h": no such file or directory
- Gbase 8C session information function (III)
- Redis persistence mechanism RDB, AOF
- Beifeng communication appeared in China (Xiamen) emergency exhibition | intelligent communication means are strong and eye-catching!
- Gbase 8C system table information function (I)
- Table custom table encapsulation
猜你喜欢

Codeforces Round #807 (Div. 2)(A-D)

网络系统实验:ping不通的问题解决

Detectron2 installation based on Anaconda under win10

There are various signs that apple is expected to support AV1

书写SQL必养成的好习惯

Intelligent OCR identification of express documents helps the logistics industry to upgrade Digitalization

泛型机制和增强for循环

The postman test interface has 404 or 500 errors when the URL is configured correctly
CA digital certificate

如何在自动化测试中使用MitmProxy获取数据返回?
随机推荐
采坑websocket總結
Redis persistence mechanism RDB, AOF
Gbase 8C session information function (II)
Gbase 8C string Operator
Summary of the fourth week of summer vacation
采坑websocket总结
GBase 8c 二进制字符串操作符
Flutter | the easiest way to add header and footer to listview
Classic example of C language - convert the input two digits into English
Semaphore
Creo 9.0 mouse button operation for model observation
MySQL client to server character set conversion
What is the function of the select... For UPDATE statement? Can you lock tables or rows?
What is promise? What are the benefits of promise
Gbase 8C system table information function (II)
Communication module sorting (II) hc-05
SAP 实施项目中涉及到编程方式操作 Excel 的几种场景介绍
Development of main applet for business card traffic near the map
Docker pulls the redis image and runs it
Gbase 8C system table information function (III)