Is jquery .bind() deprecated?

2019-02-22 04:27发布

Is jquery bind() deprecated or it can be used safely?

I see a lot of comments about bind() being deprecated in comments and answers across SO like: Jquery Event : Detect changes to the html/text of a div

Is there a JavaScript/jQuery DOM change listener?

and don't know if it is safe to use it or not (regardless of it being better or worse).

There is nothing about it being deprecated here: https://api.jquery.com/bind/

标签: jquery bind
4条回答
神经病院院长
2楼-- · 2019-02-22 04:48

No it's not, that are a difference between .on and .bind basically .bind is only called directly from a element i.e. $("#elem").bind(...), so the element must exist by the time the bind function is called. And .on can be "bind" on document i. e. $(document).on("click",".class",funcion(){...});, so if you add an element dynamically by using .append or other way, the event will be valid.

查看更多
We Are One
3楼-- · 2019-02-22 04:52

It's not deprecated in jQuery 1.1. There is actually no difference (apart form it's usage) in version jquery 1.11.3 if you inspect the declaration of the .bind event you can see it simply calls .on:

bind: function( types, data, fn ) {
    return this.on( types, null, data, fn );
},

deprecated or it can be used safely?

As others say, no it's not deprecated and is safe, it's just a bit redundant now and only exists for backwards compatibility.

Though it does seem to of been marked as deprecated in later versions of jquery now

查看更多
爷、活的狠高调
4楼-- · 2019-02-22 04:55

.bind() and .delegate() are deprecated as of jQuery 1.12 and 2.2. These functions work well with no warning message in 1.12 or 2.2, but will not work in next version and (of course) in 3.0.

It is strongly recommended to use .on() instead of .bind() and .delegate().

http://blog.jquery.com/2016/01/08/jquery-2-2-and-1-12-released/ https://github.com/jquery/jquery/issues/2288

查看更多
一夜七次
5楼-- · 2019-02-22 04:59

I don't believe its deprecated but on is the preferred method of attaching events.

http://api.jquery.com/bind/

Deprecated means they will be removing it in the future but their docs don't seem to say that.

EDIT:

As of jQuery 3.0 bind IS deprecated. The above link is still relevant but has since been updated. From their docs:

As of jQuery 3.0, .bind() has been deprecated. It was superseded by the .on() method for attaching event handlers to a document since jQuery 1.7, so its use was already discouraged.

查看更多
登录 后发表回答