开发者

Global event if a jQuery selection returns no elements?

Is there any way to globally monitor or listen to if a selection is made in jQuery and it returns no elements? E.g.:

You have a page with:

<div id=”some-id”></div>

And in a JavaScript someo开发者_开发百科ne tries to get the element but mistypes the id:

$(“#someid”)

Is there any way to globally handle when a jQuery selection returns no elements?


No. You can verify this by looking at the source that handles that case. Look into (in this case) jquery-1.4.2.js and search for the line:

                // HANDLE: $("#id")

You will see that there is no callback mechanism. It just returns if there is no result. However, the result object will always have a length (or .size()) of 0, so you can test individual results.

Your options if you want this are either to do all queries on the page with a custom function, or modify jQuery. Unfortunately, because the central function (jQuery.fn.init) is so long and has multiple purposes, there isn't a good way to do this other than to modify the jQuery file itself.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜