`
for_dream
  • 浏览: 567977 次
社区版块
存档分类
最新评论

js中的"=="和equals()以及is()方法

 
阅读更多

js中的"=="和equals()以及is()三者的区别

   一、

 

   在 javaScript或者jQuery中字符串比较没有equals()方法,要比较两个字符串是否相等可以直接用==或者is()进行判断。

 

    例如:

        

"a"=="a"  

    

$("#a").val().is("a")

  

    当然我们可以自己写一个equals()方法:

    如:

    1.

String.prototype.equals = function(s){
    return this == s;
} 

    2.

   

function equals(str1, str2)  
{  
    if(str1 == str2)  
    {  
        return true;  
    }  
    return false;  
}  

 

   二、

 

 is(expr)

  用一个表达式来检查当前选择的元素集合,如果其中至少有一个元素符合这个给定的表达式就返回true。
  如果没有元素符合,或者表达式无效,都返回'false'. 'filter' 内部实际也是在调用这个函数,所以,filter()函数原有的规则在这里也适用。

Checks the current selection against an expression and returns true, if at least one element of the selection fits the given expression.
If no element fits, or the expression is not valid, then the response will be 'false'. 'filter' is used internally, therefore all rules that apply there apply here, as well.

返回值

Boolean

参数

expr (String) :用于筛选的表达式

 

示例

由于input元素的父元素是一个表单元素,所以返回true。

HTML 代码:

<form><input type="checkbox" /></form>

 

 jQuery 代码:

$("input[type='checkbox']").parent().is("form")

 

 

结果:

  true
 
    <script language="javascript" src="jquery.js"></script>   
    <script>   
    jQuery(function($){   
        $(".abc").click(function(){   
            if ($(this).next(".content").is(":hidden")) {   
                $(this).next(".content").fadeIn("slow");   
                $(this).html("收起");   
                $(this).addClass("closeMore");   
            } else {   
                $(this).next(".content").fadeOut("slow");   
                $(this).html("打开");   
                $(this).addClass("closeMore");         
            }   
        })   
    })   
      
    </script>  
 
三、

 

  ==为js的比较运行符。

 

比较运算符

比较运算符在逻辑语句中使用,以测定变量或值是否相等。

给定 x=5,下面的表格解释了比较运算符:

运算符 描述 例子
== 等于 x==8 为 false
=== 全等(值和类型) x===5 为 true;x==="5" 为 false
!= 不等于 x!=8 为 true
> 大于 x>8 为 false
< 小于 x<8 为 true
>= 大于或等于 x>=8 为 false
<= 小于或等于 x<=8 为 true

 

 

   The 3 equal signs mean "equality without type coercion". Using the triple equals, the values must be equal in type as well.

0==false   // true
0===false  // false, because they are of a different type
1=="1"     // true, auto type coercion
1==="1"    // false, because they are of a different type

 

 

      === and !== are strict comparison operators:

JavaScript has both strict and type-converting equality comparison. For strict equality the objects being compared must have the same type and:

  • Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions.
  • Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
  • Two Boolean operands are strictly equal if both are true or both are false.
  • Two objects are strictly equal if they refer to the same Object.
  • Null and Undefined types are == (but not ===).

另:

 

1.document.getElementById
document.getElementsByName()
document.getElementsByTagName()
注意上面的Element后在Id中是没有加“s”的,特别容易写错.

 

2.注意属性选择器的使用

jQuery('[attribute="value"]')

$('input[value="Hot Fuzz"]').next().text(" Hot Fuzz");

 

分享到:
评论

相关推荐

    slow-equals:慢等于 - 比较两个字符串

    慢等于 一个快速的,比较拖曳的字符...Fastest is ===,== Compare tow equal long strings: slow-equals x 6,905,069 ops/sec ±0.74% (96 runs sampled) === x 64,830,622 ops/sec ±0.98% (96 runs sampled) == x 65,

    iswindow:检查 value 是否为 window 对象

    窗口 检查 value 是否为 window 对象...比较窗口对象时使用duck 类型和double equals,这是在IE8 中工作所必需的。 双等号是必要的,因为在 IE8 中: window . window === window ; // false window . document . pa

    js-object-clone:深度克隆并与ES5属性描述符和对象可扩展性支持进行比较

    对象克隆ES5的深克隆和比较概要// just for conveniencevar log = function ( ) { console . log . apply ( console , [ ] . slice . call ( arguments ) ) } ; var src = { name : 'dankogai' , lang : [ 'perl' ] ...

    javascript BigDecimal 大数运算类 单文件浓缩版

    本版中只有一个用于生产环境的文件:BigDecimal-all-last.min.js,大小为26K,如果WEB服务器打开GZIP压缩,可以减小至7K,如需完整版本请移步至:http://download.csdn.net/detail/aquaqu2009/4575230 简单用法: ...

    sesvc.exe 阿萨德

    众所周知 HashMap 底层是基于 数组 + 链表 组成的,不过在 jdk1.7 和 1.8 中具体实现稍有不同。 Base 1.7 1.7 中的数据结构图: 先来看看 1.7 中的实现。 这是 HashMap 中比较核心的几个成员变量;看看分别是...

    assertist:简单的 JavaScript 测试库

    断言者一个非常简单的 JavaScript 测试库。用法一个项目的规格可以被分组,以便与特定功能...测试输出地位这是一个玩具项目,旨在学习(如JavaScript Ninja书的秘密中所建议)。执照麻省理工学院。 请参阅LICENSE文件。

    java面试宝典

    116、JavaScript中的对象. 25 117、function的用法 26 118、对象的继承 27 119、this的用法 29 120、Array in JavaScript 29 121、jsp有哪些内置对象?作用分别是什么? 31 122、jsp有哪些动作?作用分别是什么? 31 123...

    千方百计笔试题大全

    172、Ajax和javascript的区别? 41 Servlet部分 42 174、JAVA SERVLET API中forward() 与redirect()的区别? 42 178、如何现实servlet的单线程模式 42 179、页面间对象传递的方法 42 180、JSP和Servlet有哪些相同点...

    angle.js:角度编程语言的javascript版本

    3”或抛出Angle具有语义索引: assert 3rd word in 'hi my friend' is 'friend' Angle按类型名称做我们所谓的匹配 To delete mail: move that mail to trash folderend 在这里,“邮件”立即充当参数名称和参数类型...

    is-equal-shallow:对两个对象进行浅表比较,如果键或值不同,则返回false

    var equals = require ( 'is-equal-shallow' ) ; // use es6 import import equals from 'is-equal-shallow' ; equals ( object_a , object_b ) ; 例子 equals ( { a : true , b : true } , { a : true , b : true...

    regent:功能性JavaScript规则引擎

    丽晶:JavaScript中的业务规则 Regent使您可以通过询问是非题来查询数据结构。 摄政逻辑被编写为单责任规则,这些规则可以自我记录,可组合并且易于阅读。 让我们来看一个例子。 import { equals } from 'regent' ...

    ng-utils:角度实用函数的 CommonJS 包装器(angular.forEach、angular.isString 等)

    angular.extend angular.equals angular.forEach angular.noop angular.bind angular.fromJson angular.identity angular.isUndefined angular.isDefined angular.isString angular.isFunction angular.isObject ...

    图书关管理系统

    用于java程序的设计和开发使用import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import java.util.*;class Student implements java.io.Serializable{String number,name,...

    无限级树(Java递归)

    * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to...

    ajax、mysql、jstl实现分页、局部刷新界面

    if (request.getParameter("currentPage") == null||request.getParameter("currentPage").equals("")) { pageContext.setAttribute("currentPage", new Integer(0)); } else { pageContext....

    Servelt技术做的E家园

    * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the ...

    messiah:节点对象验证器

    弥赛亚JS Messiah 是一个扩展的 NodeJS 对象验证器。...isLength 检查值长度是否在min和max之间。 isEmail 检查值长度是否是有效的电子邮件地址。 isMongoId 检查值长度是否是有效的 Mongo 对象 ID。 i

    整理后java开发全套达内学习笔记(含练习)

    进行高精度运算可以用java.math包中BigDecimal类中的方法。 自动类型提升又称作隐式类型转换。 强制类型转换:int ti; (byte) ti ; 强制转换,丢弃高位 宣告变量名称的同时,加上“final”关键词来限定,这个...

    Meteor-handlebar-helpers:已淘汰

    该软件包提供了一种在Meteor Spacebars模板环境中使用会话和集合的简单方法。 看一下 这里有一些简单的助手: {{$ .javascript / * arguments * /}} //新的$ script帮助器 {{$ .Session.get key}} {{$ .Session....

    list的正确remove姿势

    List mList = new ArrayList(); mList.add(1); mList.add(2); mList.add(3); mList.add(4); mList.add(5); /** ... if (mList.get(i).equals(3)) { mList.remove(i); } } printList(mList)

Global site tag (gtag.js) - Google Analytics