Javascript 如何实现jQuery';不是吗?
我有如下代码:Javascript 如何实现jQuery';不是吗?,javascript,css-selectors,Javascript,Css Selectors,我有如下代码: document.onmousedown = function(){ alert('test'); } 现在,除了ID为“box”的元素外,单击应该调用这个函数,即jQuery的.not()选择器的等价物 jQuery代码应该是: $(document).not('#box').mousedown(function(){ alert('test'); }); 如果不使用jQuery,如何实现同样的功能 编辑:我不想要jQuery代码,但我想要一个类似于Jav
document.onmousedown = function(){
alert('test');
}
现在,除了ID为“box”的元素外,单击应该调用这个函数,即jQuery的.not()
选择器的等价物
jQuery代码应该是:
$(document).not('#box').mousedown(function(){
alert('test');
});
如果不使用jQuery,如何实现同样的功能
编辑:我不想要jQuery代码,但我想要一个类似于Javascript中jQuery的.not()
选择器的操作
编辑:我正在制作一个类似addthis的小部件。它是一个10kb的文件,当选择文本时将显示一个弹出窗口。它不会使用jQuery
在我的例子中,当选择一个文本时,会显示一个弹出窗口。当单击文档而不是小部件时,小部件应该消失。要正确执行此操作,您需要检查
e.target | | e.srceelement
或其任何父项是否具有id=='box'
例如:(使用jQuery)
没有jQuery:
function isBox(elem) {
return elem != null && (elem.id === 'box' || isBox(elem.parentNode));
}
document.onmousedown = function(e) {
e = e || window.event;
if (isBox(e.target || e.srcElement))
return;
//Do things
};
或者,您可以为
box
元素处理mousedown
事件并取消冒泡。要正确执行此操作,您需要检查e.target | e.srceelement
或其任何父元素是否具有id==“box”
例如:(使用jQuery)
没有jQuery:
function isBox(elem) {
return elem != null && (elem.id === 'box' || isBox(elem.parentNode));
}
document.onmousedown = function(e) {
e = e || window.event;
if (isBox(e.target || e.srcElement))
return;
//Do things
};
或者,您可以处理
框
元素的mousedown
事件并取消冒泡。以下是一种可行的方法:
document.onmousedown = function(e){
var event = e || window.event;
var element = event.target || event.srcElement;
if (target.id !== "box") { alert("hi"); }
}
或者,如果您希望它可以与不同的ID一起重用:
function myNot(id, callback) {
return function (e) {
var event = e || window.event;
var element = event.target || event.srcElement;
if (target.id !== id) { callback(); }
}
}
使用它:
document.onmousedown = myNot("box", function () {
alert("hi");
});
以下是一种可行的方法:
document.onmousedown = function(e){
var event = e || window.event;
var element = event.target || event.srcElement;
if (target.id !== "box") { alert("hi"); }
}
或者,如果您希望它可以与不同的ID一起重用:
function myNot(id, callback) {
return function (e) {
var event = e || window.event;
var element = event.target || event.srcElement;
if (target.id !== id) { callback(); }
}
}
使用它:
document.onmousedown = myNot("box", function () {
alert("hi");
});
我能想到的最简洁的方法是设置
document.onmousedown
事件,然后在框.onmousedown
事件上停止事件传播。这避免了在整个文档中创建大量的onmousedown
事件,并且避免了每次触发事件时必须在节点的整个父层次结构中递归
document.onmousedown = function() {
alert("Foo!");
};
document.getElementById("box").onmousedown = function(e) {
alert("Bar!");
if (e.stopPropagation) {
e.stopPropagation();
} else {
e.cancelBubble = true;
}
};
我能想到的最简洁的方法是设置
document.onmousedown
事件,然后在框.onmousedown
事件上停止事件传播。这避免了在整个文档中创建大量的onmousedown
事件,并且避免了每次触发事件时必须在节点的整个父层次结构中递归
document.onmousedown = function() {
alert("Foo!");
};
document.getElementById("box").onmousedown = function(e) {
alert("Bar!");
if (e.stopPropagation) {
e.stopPropagation();
} else {
e.cancelBubble = true;
}
};
jQuery是Javascript。另外,jQuery代码也无法工作。jQuery是Javascript。另外,jQuery代码也不起作用。+1并接受了您的ans。非常感谢Slaks,您真的很棒+1并接受了你的ans非常感谢Slaks,你真的很棒!