jQuery怎么判断相等

jQuery怎么判断相等,第1张

使用恒等(===)判断(值与类型都一致)
如果用用等同(==)判断(值一致)的话有误判情况
var a = "1"; //字符串1
var b = 1; //数值1
var c = true; //布尔值1
var d = "1";
if (a==b && a==c && a==d) {consolelog('equality');} //输出equality
if (a===b || a===c) { //输出not identity
consolelog('identity');
}
else {
consolelog('not identity');
}
if (a===d) {consolelog('identity');} //输出identity

你表格里的数据哪里来的?这样的需求应该在表格数据的参数里判断啊
你非要根据颜色判断就类型这样:
<TABLE id='table1'>
<TR style="color:red">
<TD>123</TD>
<TD>123</TD>
</TR>
<TR>
<TD>123</TD>
<TD>123</TD>
</TR>
<span id='showMsg'>123</span>
</TABLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
var myTable = documentgetElementById('table1');
for(var i=0;i<myTablerowslength;i++)
{
if(myTablerows[i]stylecolor=='red')
{
documentgetElementById('showMsg')stylecolor='blue';
break;
}
}
//-->
</SCRIPT>

jquery获取标签名称:
jquery tagName prop()
如果想看某个元素是什么元素类型,只需要使用:
[object]attr("tagName")便可获取。

下面是我用jquery写的选中后立马判断选中的文件的类型,不是的话提示,然后清除,对应的可以自己改成纯js形式:

 //文件上传文件选择后事件
 $(document)ready(function() {
  $("input[id^='fileToUpload']")each(
//这里是用了each因为是多文件上传,input的id都是fileToUpload  开头
    function() {
 $("#" + $(this)attr("id") + "")live('change',function() {
 var fileName = $(this)val();
 if (fileName != null&& fileName != "") {
  //lastIndexOf如果没有搜索到则返回为-1
  if (fileNamelastIndexOf("") != -1) {
   var fileType = (fileNamesubstring(fileNamelastIndexOf("") + 1,
  fileNamelength))toLowerCase();
   var suppotFile = new Array();
   suppotFile[0] = "jpg";
   suppotFile[1] = "gif";
   suppotFile[2] = "bmp";
   suppotFile[3] = "png";
   suppotFile[4] = "jpeg";
   for ( var i = 0; i < suppotFilelength; i++) {
    if (suppotFile[i] == fileType) {
if (fileNamelength > 100) {
 alert("文件名长度不能超过100字符");
 if (!windowaddEventListener) {      
    documentgetElementById(fileName[j])outerHTML+='';  //IE清除inputfile
    }else { 
     documentgetElementById(fileName[j])value = "";   //FF清除inputfile
    } 
 return false;
}
return true;
 } else {
continue;
 }
 }
  alert("文件类型不合法,只支持 jpg、gif、png、jpeg类型!");
    if (!windowaddEventListener) {      
    documentgetElementById(fileName[j])outerHTML+='';  //IE
    }else { 
     documentgetElementById(fileName[j])value = "";   //FF
    } 
   return false;
  } else {
   alert("文件类型不合法,只支持 jpg、gif、png、jpeg类型!");
    if (!windowaddEventListener) {      
    documentgetElementById(fileName[j])outerHTML+='';  //IE
    }else { 
     documentgetElementById(fileName[j])value = "";   //FF
    } 
   return false;
  }
 }
});
});
});

在JQuery编码中,我们会判断元素是否存在某个属性比如是否包含 class="new" 的样式呢JQuery判断就非常简单了,因为有 hasClass这个方法 $("input[name=new]")hasClass("new") 即可判断
但是有时候我们需要判断别的属性,比如有的 a 链接包含 rel 属性,有的没有rel属性这时该怎么判断呢
这时就没有现成的方法了 如果存在某个属性 $("#aid")attr("rel") 会返回 rel的值,如果不存在 rel属性则会返回"undefined"
undefined 就是 undefined类型 , if($("#aid")attr("rel")=="undefined") 这个判断可能不成立
因为类型不相同
建议使用 if(typeof($("#aid")attr("rel"))=="undefined") 即可


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/13368568.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-23
下一篇 2023-07-23

发表评论

登录后才能评论

评论列表(0条)

保存