javascript 常见问题

1 apend  和  appendChild

 

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>append demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<div id="one">
    one
    <div id="two1">two1</div>
    <div id="two1">two2
        <div> three1</div>
        <div> three2</div>
    </div>
</div>
 <!-- <div id="one">
    one
    <div id="two1">two1</div>
    <div id="two1">two2
        <div> three1</div>
        <div> three2</div>
    </div>
    <div>div Hello1</div>
    <div>div hello2</div>
</div> -->

<!--
    append和appendchild主要区别在于它appendchild是一个dom方法,append是一个jquery方法。第二个使用第一个,你可以在jquery源代码上看到
    append: function() {
        return this.dommanip(arguments, true, function( elem ) {
        if ( this.nodetype === 1 || this.nodetype === 11 || this.nodetype === 9 ) {
            this.appendchild( elem );
            }
        });
    } 
-->


<!--
now append是JavaScript中的一种方法

引用MDN
该ParentNode.append方法DOMString在最后一个子节点之后插入一组Node对象或对象ParentNode。DOMString对象作为等效的Text节点插入。

JavaScript的追加类似于jQuery的追加。

该ParentNode.append方法在最后一个子节点之后插入一组Node对象或DOMString对象ParentNode。DOMString对象作为等效Text节点插入。
差异Node.appendChild():
ParentNode.append()允许您也附加DOMString对象,而Node.appendChild()只接受Node对象。
ParentNode.append()没有返回值,而Node.appendChild()返回附加的Node对象。
ParentNode.append()可以附加多个节点和字符串,而Node.appendChild()只能附加一个节点。
 -->

<script>
$("#one").append("<div>div Hello1</div>"); 
var div = document.createElement("div");div.innerText="div hello2"
document.getElementById("one").appendChild(div) 

/*  ParentNode.append  */
var parent = document.createElement("div");
var p = document.createElement("p");
parent.append(p);

 //上面都是将新元素添加到直属子元素的最后  对于children 可以认为是孩子元素。而不是孙子grandChildren元素

 console.log(document.getElementById("one").children)//获得他的所有直属子元素

</script>
 
</body>
</html>

2 js比较数字

2018-09-05 16:43:39

var a=1;var b=1.0000
console.log(a==b)//true
console.log(a>b)//false
console.log(a<b)//false
console.log(typeof a)//number
console.log(typeof b)//number

console.log("2">"1")//true

console.log("1"=="1")//true
console.log("1">"1")//false
console.log("1"<"1")//false

console.log("1"=="1.00")//false
console.log("1">"1.00")//false
console.log("1"<"1.00")//true   值相同的数字字符串 整数总数小于小数
console.log(parseFloat("1.00")==parseInt("1"))//true

console.log("1">"0.9")//true

console.log("1.00"=="1.0000")//false
console.log("1.00">"1.0000")//false
console.log("1.00"<"1.0000")//true  值相同的数字字符串 小数点后的位数越多的越大


JS在比较数字时候若两边都是整数可以直接比较。
若有一边是小数 则需要判断两边是否都为number类型
    是,可以比较,
    否,需要转换类型再比较


JS里面有两种转换的,parseFloat和parseInt。
比较的时候想下面这样
整数如下:
if(parseInt(xxx)>parseInt(xxxx)){
}
小数可以这样。
if(parseFloat(xxx)>parseFloat(xxxx)){
}
这样其实也可以
if(parseInt(xxx)>parseFloat(xxxx)){
}
转换出来都是number类型
console.log(typeof parseFloat("1.00"))//number
console.log(typeof parseInt("1"))//number

3 禁止滚动条

2018-09-07 16:36:13
<body style="overflow: hidden" οnscrοll="no">

 

4

 2018-08-31 19:17:39
1
建议不要在条件表达式中使用简单赋值,因为在浏览代码时,赋值可能与相等性混淆。例如,不要使用以下代码:
if (x = y) {
   /* do the right thing */
}
如果需要在条件表达式中使用赋值,通常的做法是在赋值周围添加其他括号。例如:
if ((x = y)) {
   /* do the right thing */
}


if (x = y) {
   /* do the right thing */
}

if (x == y) {
   /* do the right thing */
}
效果一样

5

Do not confuse the primitive boolean values true and false with truthiness or falsiness of the Boolean object. 
Any value that is not false, undefined, null, 0, NaN, or the empty string (""), and any object, including a Boolean object whose value is false, is considered truthy when used as the condition. For example:
Any value, including a Boolean object whose value is false, is considered truthy when used as the condition. For example:
var x = anyone
js中建议用 if(!x) 判空
对于if  false, undefined, null, 0, NaN, 空字符串
var b = new Boolean(false);
if (b) // this condition is truthy

 

6

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>jQuery.extend demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>

<body>
    2018-09-20 21:05:07
</body>
<script>
    alert(Array.isArray("[1, 2]"));//F
    alert(Array.isArray([1,2]));//T
    alert(Array.isArray("1,2".split(",")));//T
    alert("a,2".indexOf(","));//1
    alert(typeof("123"));//string

    //定义对象
    var xx = {};
    xx.a = "hello";
    alert(xx.a);
</script>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值