首页 > 其他分享 >JS_7_常用方法和对象

JS_7_常用方法和对象

时间:2023-01-14 18:57:09浏览次数:40  
标签:常用 对象 JS write 获取 字符串 date document Math

JS开发者提供的对象方法。 

 


一、字符串操作

常用操作:

  大小写转换、截取、查找。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>学习使用类</title>
        <script type="text/javascript">
            //转为大写
            document.write("aaa".toUpperCase());
            document.write("<br/>");
            //转为小写
            document.write("AAA".toLowerCase());
            document.write("<br/>");
            //截取指定开始位置,指定数量的字符
            document.write("abcdefg".substr(0,2));
            document.write("<br/>");
            //截取指定开始位置,结束位置的字符串
            document.write("abcdefg".substring(2,4));
            document.write("<br/>");
            
            //首字母大写
            var str = "abcdefg";
            document.write(str.substring(0,1).toUpperCase()+str.substring(1,7).toLowerCase());
            document.write("<br/>");
            
            //从开头查找字符位置
            document.write("abcdefg".indexOf('d'));
            document.write("<br/>");
            //从结尾查找字符位置
            document.write("abcdefg".lastIndexOf('d'));
            document.write("<br/>");
            
            
        </script>
    </head>
    <body>
    </body>
</html>
View Code

二、日期对象

常用操作:

  获取年月日时分秒、设置年月日时分秒、获取时间戳。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>学习使用类</title>
        <script type="text/javascript">
            var date = new Date();
            //获取年、月、日
            document.write(date.getFullYear()+"年"+(date.getMonth()+1)+"月"+date.getDate()+"日");
            document.write("<br/>");
            //获取星期
            document.write(date.getDay());
            document.write("<br/>");
            //获取时分秒
            document.write(date.getHours()+":"+date.getMinutes()+":"+date.getSeconds());
            document.write("<br/>");
            //获取时间戳
            document.write(date.getTime());
            document.write("<br/>");
            
        </script>
    </head>
    <body>
    </body>
</html>
View Code

三、数学对象Math

常用操作:

  常用函数、获取随机数、取整。

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>学习</title>
            <script type="text/javascript">
                //获取[0-1)之间的随机数
                document.write(Math.random());
                document.write("<br/><br/><br/>");
                
                //获取10到20之间的随机数
                var numb = Math.random()*10+10;
                document.write(numb);
                document.write("<br/><br/><br/>");
                
                //四舍五入取整数
                document.write(Math.round(numb));
                document.write("<br/><br/><br/>");
                
                //向上取整数
                document.write(Math.ceil(numb));
                document.write("<br/><br/><br/>");
                
                //向下取整数
                document.write(Math.floor(numb));
                document.write("<br/><br/><br/>"); 
            </script>
        </head>
        <body>
        </body>
    </html>
View Code

四、Global对象

常用操作:

  把字符串作为js代码执行、判断一个值是否不为数字、字符串转整数浮点数。

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>学习</title>
            <script type="text/javascript">
                
                //把字符串作为js代码执行
                eval("document.write(Math.random());");
                document.write("<br/><br/><br/>");
                
                //判断一个变量被Number()转换后,是否不为数字
                document.write(isNaN("ad"));
                document.write("<br/><br/><br/>");
                
                //从头开始,把字符串转换为尽可能的整数
                document.write(parseInt("12.22a2b3cc"));
                document.write("<br/><br/><br/>");
                
                //从头开始,把字符串转换为尽可能的浮点数
                document.write(parseFloat("12.22a2b3cc"));
                document.write("<br/><br/><br/>");
                
            </script>
        </head>
        <body>
        </body>
    </html>
View Code

 

标签:常用,对象,JS,write,获取,字符串,date,document,Math
From: https://www.cnblogs.com/lurenjia-bky/p/17052359.html

相关文章

  • js常用库
    操作时间moment.jsDay.js(轻量级)处理URLqs.js操作cookie1.js-cookie.js处理数组、对象、字符串Lodash调试移动端Vconsole......
  • Linux实际常用命令
    解决Linux关闭终端(关闭SSH等)运行的程序被迫停止使用nohup命令,不挂起,不挂断,后台运行。nohungup。比如正常情况下要运行:pythonmain.py让它后台运行不挂断,就运行:n......
  • Python常用文件操作程序
    批量修该文件名。importos#导入模块filename='./2212144sep'#文件地址list_path=os.listdir(filename)#读取文件夹里面的名字st=#写入开始forindexin......
  • XML及JSON扩展方法,方便快速解析
    #regionXML扩展方法///<summary>///从xml节点中获取指定属性的数据,如果不存在该属性则返回默认值///</summary>///<typeparamname="T">xml数据的数据类型</type......
  • 20 个 JS 工具函数助力高效开发
    日常开发中,面对各种不同的需求,我们经常会用到以前开发过的一些工具函数,把这些工具函数收集起来,将大大提高我们的开发效率。1、校验数据类型export const typeOf = fu......
  • 个人常用软件记录
    B站直播录制和编码:B站录播姬B站视频下载:DownKyim3u8视频下载:N_m3u8DL-CLI_v3.0.2视频快速剪辑:LosslessCut视频字幕制作:Aegisub视频字幕压制:ShanaEncoder音视频处理工......
  • 对象和值
    对象是一个名字,一个标识符,标识了一段连续的内存之所以要对两个概念有区别,是为了统一C语言的语法解释,方便记忆理解赋值语句的含义赋值语句的语法解释inta=1其实是告......
  • 利用lodash对(对象)数组去重
    使用场景:根据数(对象)组中的id或者其他属性去重,或者对象中的所有属性值相同的去重。传统方法:通过数组的some进行逐项判断;用了lodash之后发现还是很香的。import{isEqual......
  • Centos7.6安装nodejs
    1.直接yuminstallnodejs安装后没有npm,搜索后说时要先执行curl--silent--locationhttps://rpm.nodesource.com/setup_10.x|bash-,再进行yuminstall,因内网环境就放......
  • python中os.path常用属性和部分使用方法
    1os.path模块常用的属性和方法2简单示例在一个工程或者项目中用到的会多一点,比如需要得到某个文件或者配置文件的路径等。这里示例为:C:\Users\Administrator\Desk......