js中的"=="和equals()以及is()三者的区别
来源:互联网 发布:ascii码排序 c语言 编辑:程序博客网 时间:2024/06/08 16:53
一、
在 javaScript或者jQuery中字符串比较没有equals()方法,要比较两个字符串是否相等可以直接用==或者is()进行判断。
例如:
当然我们可以自己写一个equals()方法:
如:
1.
- String.prototype.equals = function(s){
- return this == s;
- }
2.
- function equals(str1, str2)
- {
- if(str1 == str2)
- {
- return true;
- }
- return false;
- }
二、
is(expr)
返回值
Boolean
参数
expr (String) :用于筛选的表达式
示例
由于input元素的父元素是一个表单元素,所以返回true。
HTML 代码:
- <form><input type="checkbox" /></form>
jQuery 代码:
- $("input[type='checkbox']").parent().is("form")
结果:
- <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 // true0===false // false, because they are of a different type1=="1" // true, auto type coercion1==="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()三者的区别
- js中的"=="和equals()以及is()三者的区别
- js中的"=="和equals()以及is()三者的区别
- js中的"=="和equals()以及is()三者的区别
- "=="和equals和equalsIgnoreCase三者的区别
- ==,equals(),hashCode()三者的区别和联系
- equalsIgnoreCase()和equals()以及'=='的区别以及使用,
- java中的栈、堆以及equals和==的区别
- java中==和equals的区别以及java中的堆栈
- java中的栈、堆以及equals和==的区别
- Java的数据类型以及equals和==的区别
- 黑马程序员----------equals和==的区别以及用法
- java中 “==”和“equals”以及instanceof的区别
- 黑马程序员-equals和==的用法以及区别
- null和""以及==与equals的区别
- null和""以及==与equals的区别
- Java equals 方法以及和 ==的区别
- Java中的equals和==和equalsIgnoreCase()的区别
- Junit断言的学习
- Android 自定义控件3
- 鼠标双击桌面上的快捷方式变成属性
- Launcher3 开机后应用数据的加载流程分析
- 设计模式系列3---搞转换的适配器模型
- js中的"=="和equals()以及is()三者的区别
- jquery中怎样把字符串解析成json【前端$.parseJSON(字符串)返回json对象详细看文章内容】
- hdu2037今年暑假不AC
- Akka并发编程——第二节:Actor模型(一)
- 属性动画实现卫星菜单
- 哈夫曼编译码器的实现 使用文件
- 杭电1285 确定比赛名次(拓扑排序)
- Java工程师成神之路
- Git使用-查看提交日志