jquery、js基础

4 篇文章 0 订阅
3 篇文章 0 订阅

一、对checkbox操作:

(1)获取所有选中和未选中的checkbox:

var notCheckedArray = $("input:checkbox").not("input:checked") ;//获取未选中的checkbox
var checkedArray = $("input:checked");//获取选中的checkbox

(2)遍历所有选中和未选中的checkbox的value值:

$(checkedArray).each(function(){ 
   alert("选中复选框的值:"+$(this).val()); 
});
$(notCheckedArray).each(function(){ 
   alert("未选中复选框的值:"+$(this).val()); 
});

(3)将(2)中的value值加入数组当中:

var notCheckedValArray = new Array();  //存放未选中的checkbox的value值
var checkedValArray = new Array();   //存放选中的checkbox的value值
for (var i = 0; i < checkedArray.length; i++){   
   checkedValArray[i] = checkedArray[i].value;   
}
for (var i = 0; i < notCheckedArray.length; i++){   
   notCheckedValArray[i] = notCheckedArray[i].value;   
}

二、判断为空:

if(tt==null || tt==""){  
    alert("kong");  
} else {  
    alert("bukong");  
}
数组某个元素是否为空
1. 如果是数组元素不存在: if (arr[x] == undefined) 判断

2. 如果是元素里的内容为空值,那就需要根据你填充数组的值来做判断。

var b = new Array (1,null,'')
console.log(b[1] == null); //填充值为null的元素
console.log(b[2] == ''); //填充值为空('')的元素
console.log(b[3] == undefined); //判断不存在的元素

三、给json动态添加元素:

var json={};
json[key1] = value1;

四、Jquery判断Id是否存在

如果是下面的 jQuery 代码判断一个对象是否存在,是不能用的

if($("#id")){
}else{}

因为 $(“#id”) 不管对象是否存在都会返回 object 。

正确使用判断对象是否存在应该用:

if($("#id").length>0){}else{}

使用 jQuery 对象的属性 length 来判断,如果 > 0 就存在。

或者

 if($("#id")[0]){} else {}

或者直接使用原生的 Javascript 代码来判断:

 if(document.getElementById("id")){} else {}

二、根据父节点查找子节点
jQuery之children()返回匹配对象的字节点
children()  返回匹配对象的子介点
<p>one</p>
<div id=”ch”>   
     <span>two</span>
</div>
jQuery代码及功能:
function jq(){
    alert($(“#ch”).children().html());
}
$(“#ch”).children()得到对象[ <span>two</span> ].所以.html()的结果是”two”

三、根据子节点查找父节点

<div id=”ch”>   
      <span>two</span>
      <span id=”sp”>three</span>
</div>


jQuery代码及功能
Jquery.ready ({
    alert($(“#ch”).children(“#sp”).html());
});
$(“#ch”).children()得到对象[<span>two</span><span id="sp">three</span> ].
$(“#ch”).children(“#sp”)过滤得到[<span id="sp">three</span> ]

JS中实现replaceAll的方法

第一次发现JavaScript中replace() 方法如果直接用str.replace("-","!") 只会替换第一个匹配的字符. 
而str.replace(/\-/g,"!")则可以全部替换掉匹配的字符(g为全局标志)。 

replace() 
The replace() method returns the string that results when you replace text matching its first argument 
(a regular expression) with the text of the second argument (a string). 
If the g (global) flag is not set in the regular expression declaration, this method replaces only the first 
occurrence of the pattern. For example, 

var  s = "Hello. Regexps are fun." ;s = s.replace(/\./, "!" ); // replace first period with an exclamation pointalert(s);

produces the string “Hello! Regexps are fun.” Including the g flag will cause the interpreter to 
perform a global replace, finding and replacing every matching substring. For example, 

var  s = "Hello. Regexps are fun." ;s = s.replace(/\./g, "!" ); // replace all periods with exclamation pointsalert(s);

yields this result: “Hello! Regexps are fun!” 

所以可以用以下几种方式.:
string.replace(/reallyDo/g, replaceWith);
string.replace(new RegExp(reallyDo, 'g'), replaceWith);

string:字符串表达式包含要替代的子字符串。
reallyDo:被搜索的子字符串。
replaceWith:用于替换的子字符串。

Js代码   收藏代码
  1. <script type="text/javascript">  
  2. String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {  
  3.     if (!RegExp.prototype.isPrototypeOf(reallyDo)) {  
  4.         return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi""g")), replaceWith);  
  5.     } else {  
  6.         return this.replace(reallyDo, replaceWith);  
  7.     }  
  8. }  
  9. </script>  



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值