Make jQuery throw error when it doesn't match an element

Make jQuery throw error when it doesn‘t match an element

解答1

You could make a plugin to use to ensure that the jQuery object is not empty:

$.fn.ensure = function() {
  if (this.length === 0) throw "Empty jQuery result."
  return this;
}

Usage:

$(‘ul.some-list‘).ensure().append(listItem);

解答2

In my case, I wanted it automatically applied to everything, so I modified the jQuery constructor. I suggest you only use this during development to catch mistakes, but keep it out of your production so as not to confuse others. As it‘s not an error, I‘ve just used console logging. However I‘ve added a commented out throw in case you would rather use that:

var jQueryInit = $.fn.init;

$.fn.init = function(arg1, arg2, rootjQuery) {
    var element = new jQueryInit(arg1, arg2, rootjQuery);
    if (arg1 && element.length === 0) {
        console.log(arg1 +" doesn‘t exist");
        // throw arg1 +" doesn‘t exist"
    }
    return element;
};

I really wish there was an option in jQuery for turning this on during dev.

In case anybody is wondering... having the selector return a result set, when there are no valid matches, is a deliberate decision by the developers of jQuery. It means you can apply some code to a collection of elements, even if there is a possibility of there being none. A good example is comments on a page, or checked checkboxes. If it threw an error when there were no were elements found, then you would have to add checks for existence to your code. The following topic has more info: https://stackoverflow.com/a/3709823/109561

This is great feature for keeping production code lean. However it can really bite you in the ass while developing if you make a small typo; or if you try selecting an ID named "foobar" when you should be selecting a class called "foobar".

Make jQuery throw error when it doesn't match an element

原文地址:https://www.cnblogs.com/chucklu/p/11131024.html

时间: 2024-10-28 13:51:10

Make jQuery throw error when it doesn't match an element的相关文章

ajax的status为201依然触发jquery的error事件的问题

昨天在调试一个ajax的时候发现,即使status是201,仍然会触发jquery的error事件.statusText是"parseerror". 通过在stackoverflow上查询,戳这里. 加上参数 dataType:"text" 就可以解决. 而问题产生的原因在评论里写的也很详细.但是并不对,他弄错了convert的位置,在jquery判断状态码前,就已经进行了ajaxConvert. // Determine if successful,判断statu

IE8"HTML Parsing Error:Unable to modify the parent container element before the child element is closed"错误

一.IE8报下面错误,解决办法:网页错误详细信息消息: HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917)行: 0字符: 0代码: 01.查看是否有未关闭的html标签,比如<table>而没有</table> 2.是否在页面未加载完前js代码操作了body里的元素,将相关js代码移到</body>后

HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917)

IE8报错误: 用户代理: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET4.0C; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0E)时间戳: Mon, 13 Oct 2014 00:54:55 UTC 消息: HTML P

git——创建分支后,切换分支报错(error: pathspec &#39;master&#39; did not match any file(s) known to git)

error: pathspec 'master' did not match any file(s) known to git 解决办法: 1.查看分支 git branch -a 2.获取所有分支 git fetch 3.切换到远程master分支: git checkout origin/master 4.执行git branch,可以看到我们想切换的那个分支 5.从当前的分支切换并新建分支,可以理解为即将新创建的分支是由当前分支出来的 git checkout -b 新分支名 6.建立本地

jQuery ajax error:parserror

前端jQuery ajax(部分): $.ajax({type:"post",dataType:'json',url:"http://localhost:8080/Authorization/UsernameConfirm.jsp",data:{//传参 "username":$("#username").val(),}, success:function(msg){ $("#showResult").ht

jquery ajax error函数详解

代码:$(document).ready(function() {            jQuery("#clearCac").click(function() {                jQuery.ajax({                    url: "/Handle/Do.aspx",                    type: "post",                    data: { id: '0' }

jquery ajax error函数和及其参数详细说明 (转)

引用地址:https://blog.csdn.net/u014756827/article/details/52950514 使用jquery的ajax方法向服务器发送请求的时候,常常需要使用到error函数进行错误信息的处理,本文详细的说明了ajax中error函数和函数中各个参数的用法. 一般error函数返回的参数有三个: function(jqXHR jqXHR, String textStatus, String errorThrown).常见调用代码如下: $.ajax({ url:

jQuery ajax error函数的参数(交互错误信息的获取)

一般error函数返回的参数有三个: function(jqXHR jqXHR, String textStatus, String errorThrown).常见调用代码如下: $.ajax({ url: '/Home/AjaxGetData', success: function (data) { alert(data); }, error: function (jqXHR, textStatus, errorThrown) { /*错误信息处理*/ } }); 这里对这三个参数做详细说明.

JQuery AJAX error参数

$.ajax({ url: "/api/v1/XMProject", //请求的url地址 dataType: "json", async: true, data: data, type: "POST", success: function (reg) { if (reg.code == 0) { parent.location.href = "/project/xmlist.html"; } else { layer.msg