检测jQuery-selector是否返回空对象的最佳方法是什么.如果你这样做:
alert($('#notAnElement'));
Run Code Online (Sandbox Code Playgroud)
你得到[object Object],所以我现在这样做的方式是:
alert($('#notAnElement').get(0));
Run Code Online (Sandbox Code Playgroud)
这将编写"未定义",因此您可以检查它.但看起来非常糟糕.还有什么其他方式?
我正在使用jQuery验证插件进行客户端验证.editUser()单击"编辑用户"按钮调用该功能,该按钮显示错误消息.
但是我想在我的表单上清除错误消息,当我点击"清除"按钮时,它会调用一个单独的功能clearUser().
function clearUser() {
// Need to clear previous errors here
}
function editUser(){
var validator = $("#editUserForm").validate({
rules: {
userName: "required"
},
errorElement: "span",
messages: {
userName: errorMessages.E2
}
});
if(validator.form()){
// Form submission code
}
}
Run Code Online (Sandbox Code Playgroud) 我试图了解jQuery在搜索DOM元素时如何创建返回对象.我已经浏览了消息来源,但我并不完全确定我理解,并且希望有人在这里能给我一些见解.
从我可以收集的内容中读取源代码,在查询jQuery DOM时,jQuery找到匹配的DOM元素,然后使用元素的索引作为新对象的键,将匹配的DOM元素添加为对象.
if ( rsingleTag.test( match[1] ) && jQuery.isPlainObject( context ) ) {
for ( match in context ) {
// Properties of context are called as methods if possible
if ( jQuery.isFunction( this[ match ] ) ) {
this[ match ]( context[ match ] );
// ...and otherwise set as attributes
} else {
this.attr( match, context[ match ] );
}
}
}
return this;
Run Code Online (Sandbox Code Playgroud)
返回this,返回包含所有方法的整个jQuery对象.我是否正确到了这一点?
现在,它出现了所有的功能,如css,find,ajax,hide等.在jQuery.fn对象中.
不知何故(我认为这是我没有看到的地方),这些函数被调用,而不是在DOM元素本身,而是通过access.js https://github.com/jquery/jquery/blob/master/ SRC /核心/ access.js
var access = jQuery.access …Run Code Online (Sandbox Code Playgroud) 我已经使用Java很长一段时间了,很长一段时间我一直在使用GWT(Google Web Toolkit)进行Web开发.它的美妙之处在于我拥有了面向Java对象的构造,并且不会担心它如何被转换为GWT - 让Google来处理它.我对Javascript的了解已经足够,但并不是因为我可以用它进行大量的Web开发.后来我决定我必须对Javascript有更深入和更彻底的了解,这是一个真正的过山车 - 只是在我认为我得到了一些东西,有些东西来证明我错了 - 我只是被误解了.
有什么比stackoverflow更能表达我的关注的地方:我正在寻找一些资源和指向什么是Javascript等同于以下一些Java概念:
Class
instance of a class - object
Member variables
Getters
Setters
Abstract Class
Interface
Inheritance
Access Modifiers
Constructors
Run Code Online (Sandbox Code Playgroud)
我知道其中一些概念,但正如我所说 - 我相信我有一些概念上的困难.如果有人可以指出一个真正的javascript大师试图在这里查明这些概念,我将非常高兴.
我将Colorbox.min.js文件从v1.3.19更新为v1.4.6,并且我的一些颜色框不起作用.我在页面和Chrome的控制台上没有任何错误.
我检查了更改日志,但我没有找到anser.你能帮帮忙吗?
(我使用jQuery 1.7.2)
<a href="#" onclick="emailDialog()">e-mail</a>
Run Code Online (Sandbox Code Playgroud)
function emailDialog(){
$.fn.colorbox({
width:"700px", height:"550px",
iframe:true, href:"/contact",
opacity:0.6
});
}
Run Code Online (Sandbox Code Playgroud)
<a href="http://example.com/1.jpeg" class="colorbox-avatar" title="some title" rel="nofollow" >photo</a>
Run Code Online (Sandbox Code Playgroud)
$(document).ready(function() {
$(".colorbox-avatar").colorbox({
rel:'colorbox-avatar',
scrolling: false,
current: "",
slideshow:true, slideshowAuto:false,
opacity:0.6,
width:"60%" , height:"60%"
});
}
Run Code Online (Sandbox Code Playgroud) 根据这个StackOverflow的答案,jQuery.fn是什么意思?,fn属性in jQuery.fn.jquery是prototype属性的别名.我假设在完整代码如下的这两种方法中这是相同的
$.fn.map = function() 和 $.fn.tweets = function()
我的问题是,例如,如果$ .fn.tweets使用原型来创建推文方法,那么这段代码是否$('tweets').tweets 会调用它...
var $tweets = $('#tweets').tweets({
query: buildQuery(approxLocation),
template: '#tweet-template'
});
Run Code Online (Sandbox Code Playgroud)
如果是这样,它将如何触发该方法.例如,仅仅在文件加载上创建变量是否会触发该函数,其中包含其他方法,即查询?谢谢你的帮助
完整的方法代码
$.fn.map = function(method) {
console.trace();
console.log(method);
if (method == 'getInstance') {
console.log("fn.map");
return this.data('map');
}
return this.each(function() {
var $this = $(this);
var map = $this.data('map');
if (map && MyMap.prototype[method]) {
map[method] (Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
var options …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
$.extend($.fn.dataTableExt.oSort, {
"datetime-uk-pre": function (a) {
from = a.split(' ');
var ukDatea = from[0].split('/');
var ukTimea = from[1].split(':');
return (ukDatea[2] + ukDatea[1] + ukDatea[0] + ukTimea[1] + ukTimea[0]) * 1;
},
"datetime-uk-asc": function (a, b) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"datetime-uk-desc": function (a, b) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
});
$.extend($.fn.dataTableExt.oSort, {
"date-uk-pre": function (a) {
var …Run Code Online (Sandbox Code Playgroud)