自定义属性
设置元素属性值:element.setAttribute(" "," ");可设置内置属性也可设置自定义属性
<body>
<input type="text" value="你好" data-index="0" />
</body>
<script>
const input = document.querySelector("input");
input.value= "hello";
console.log(input.value); // "hello"
input.setAttribute("value", "hi");
console.log(input.getAttribute("value")); // "hi"
input.setAttribute("data-index", "1");
console.log(input.getAttribute("data-index")); // "1"
input.setAttribute("data-name", "shy");
console.log(input.getAttribute("data-name")); // "shy"
</script>
获取元素属性值:element.getAttribute(" "," ");可获取内置属性也可获取自定义属性
<body>
<input type="text" value="你好" data-index="0" />
</body>
<script>
const input = document.querySelector("input");
console.log(input.value); // "你好"
console.log(input.getAttribute("value")); // "你好"
console.log(input.getAttribute("data-index")); // "0"
</script>
移除元素属性值:removeAttribute("属性名")可移除内置属性,也可移除自定义属性
<body>
<input type="text" value="你好" data-index="0" />
</body>
<script>
const input = document.querySelector("input");
input.removeAttribute("value");
input.removeAttribute("data-index");
</script>
向上取整函数:Math.ceil(double a); 向下取整函数:Math.floor(double a);
判断回文数
方法一:StringBuff
public class Main {
public static void main(String[] args) {
Scanner a=new Scanner(System.in);
String b=a.next();
StringBuffer c=new StringBuffer(b);
c.reverse();
String d=new String(c);
if(d.equals(b)) {
System.out.println("是回文数");
}
else System.out.println("不是回文数");
}
}
方法二:
public class Main {
public static void main(String[] args) {
Scanner a=new Scanner(System.in);
int b=a.nextInt();
int x,sum=0,num;
num=b;
while (b!=0) {
x=b%10;
b/=10;
sum=sum*10+x;
}
if(sum==num) {
System.out.println("是回文数");
}
else System.out.println("不是回文数");
}
}
四舍五入:
System.out.println(String.format("%.0f",7.0/2)); //4
System.out.println(String.format("%.0f",7.0/3)); //2
//若逗号后无浮点型数字要先将类型转换为float eg:(float)
进制转换:
int k=37,r=8;
System.out.println(Integer.toString(k,r));//十进制转换为r进制
System.out.println(Integer.parseInt("11",8));//其他进制转换为十进制
超大数运算:
public class Main {
public static void main(String[] args) {
BigInteger bigInteger1=new BigInteger("333333333333");
BigInteger bigInteger2=new BigInteger("444444444444");
System.out.println("两个数分别是"+bigInteger1+"和"+bigInteger2);
//BigInteger类型数字的四则运算
//加
System.out.println(bigInteger1.add(bigInteger2));
//减
System.out.println(bigInteger1.subtract(bigInteger2));
//乘
System.out.println(bigInteger1.multiply(bigInteger2));
//除
System.out.println(bigInteger1.divide(bigInteger2));
}
}
取值范围:
int类型:10的9次方
long类型:10的18次方