首页 > web前端 > js教程 > 正文

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

高洛峰
发布: 2016-12-16 09:32:03
原创
5438人浏览过

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

例如:        

"a"=="a"      

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

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

 如:

Js代码 

动态WEB网站中的PHP和MySQL:直观的QuickPro指南第2版
动态WEB网站中的PHP和MySQL:直观的QuickPro指南第2版

动态WEB网站中的PHP和MySQL详细反映实际程序的需求,仔细地探讨外部数据的验证(例如信用卡卡号的格式)、用户登录以及如何使用模板建立网页的标准外观。动态WEB网站中的PHP和MySQL的内容不仅仅是这些。书中还提到如何串联JavaScript与PHP让用户操作时更快、更方便。还有正确处理用户输入错误的方法,让网站看起来更专业。另外还引入大量来自PEAR外挂函数库的强大功能,对常用的、强大的包

动态WEB网站中的PHP和MySQL:直观的QuickPro指南第2版 508
查看详情 动态WEB网站中的PHP和MySQL:直观的QuickPro指南第2版
String.prototype.equals = function(s){  
    return this == s;  
}
登录后复制

Js代码 

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 代码:

Html代码 

<form><input type="checkbox" /></form>
登录后复制

 jQuery 代码:

Js代码 

$("input[type='checkbox']").parent().is("form")
登录后复制

结果:

  true

 

Js代码 

<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");
登录后复制

更多js中的"=="和equals()以及is()方法相关文章请关注PHP中文网!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号