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

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

例如:

"a"=="a"

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

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

如:

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

jQuery 代码:

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

结果:

true

Js代码

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");

}

})

})

==为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中文网!

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值