jQuery 如果 .parent() .hasclass 然后不应用该函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9002148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
if .parent() .hasclass then do not apply the function
提问by mcmwhfy
I have this function which should be called only when parent doesn't have the class cooling
.
我有这个函数,只有当 parent 没有 class 时才应该调用它cooling
。
function inputWidth() {
$('input[disabled="disabled"]').removeAttr("disabled").prop("readonly", true);
$('input[type!="submit"]').focus(function(){
if ($(this).val().length > 20) {
$(this).attr('data-default', $(this).width());
$(this).animate({
width: 350
}, 'slow');
$(this).parent().addClass('cooling');
}
}).blur(function(){ /* lookup the original width */
var w = $(this).attr('data-default');
$(this).animate({
width: w
}, 'fast');
$(this).parent().removeClass('cooling');
});
};
回答by xbonez
if (!$(this).parent().hasClass('cooling'))
{
//your function
}
回答by PriorityMark
I'm thinking this is being overanalyzed. There's no reason to add an extra class to the parent (unless you're doing this for styling, in which case feel free to add it back in).
我认为这是被过度分析了。没有理由向父类添加额外的类(除非您这样做是为了样式化,在这种情况下可以随意添加它)。
Basically we only set the default data value once, and if it's there we don't set it again (we just reuse it).
基本上我们只设置一次默认数据值,如果它在那里,我们不会再次设置它(我们只是重用它)。
You'll also notice the use of $.data() instead of .attr(). Try to avoid .attr(). It's not all that efficient for this sort of thing as you're basically asking it to lookup or store the value in the actual DOM (manipulating the DOM is very expensive). In most cases it's not the proper usage anyways. In your code, $(this).data("default") is the proper way to get at what you want. As an example:
您还会注意到使用了 $.data() 而不是 .attr()。尽量避免 .attr()。对于这类事情,它并不是那么有效,因为您基本上要求它在实际 DOM 中查找或存储值(操作 DOM 非常昂贵)。在大多数情况下,它无论如何都不是正确的用法。在您的代码中, $(this).data("default") 是获得所需内容的正确方法。举个例子:
<input type="text" id="username" data-item="random data" />
using jQuery to get at data-item you'd call:
使用 jQuery 获取您要调用的数据项:
$("#username").data("item");
and get back "random data"
.
回来"random data"
。
One last thing, when using animations (especially 'slow' animations) you should make it a habit to call stop first, that way any animations that are in progress are halted immediately before starting the next animation.
最后一件事,当使用动画(尤其是“慢”动画)时,你应该养成先调用停止的习惯,这样任何正在进行的动画都会在开始下一个动画之前立即停止。
$('input[disabled="disabled"]').removeAttr("disabled").prop("readonly", true);
$('input').focus(function() {
if ($(this).val().length > 20) {
$(this).data('default', $(this).data('default') || $(this).width());
$(this).stop().animate({
width: 300
}, 'slow');
}
}).blur(function() { /* lookup the original width */
$(this).stop().animate({
width: $(this).data('default')
}, 'fast');
});
Here's a jsfiddlefor you as well.
这里还有一个jsfiddle给你。
I hope I've helped you, all that said, keep in mind that your original question was answered correctly by several other people on here. They all provided the correct code for not executing something if the parent has a particular class. In the future, when asking questions, please be careful to articulate exactly what issue you're having. You were asking something along the lines of "how can I toggle size based upon focus" not "if parent hasclass then do not apply...". The other answers did answer your question. The problem is you weren't asking the right question.
我希望我已经帮助了你,所有这一切,请记住,这里的其他几个人已经正确回答了你最初的问题。如果父级具有特定类,它们都提供了不执行某些内容的正确代码。将来,在提出问题时,请小心准确地说明您遇到的问题。您问的是“我如何根据焦点切换大小”而不是“如果父类具有类则不适用......”。其他答案确实回答了您的问题。问题是你没有问正确的问题。
回答by Sagar Patil
Try this:
尝试这个:
if( !$(this).parent().hasClass('cooling') ){
inputWidth();
}
回答by m_vitaly
You want to set the data-default
only once, so I would go with this solution:
你想data-default
只设置一次,所以我会选择这个解决方案:
...
if (typeof $(this).attr('data-default') == 'undefined') {
$(this).attr('data-default', $(this).width());
}
...
回答by Pievis
As an alternative to previous answers: sometimes it might be more clean to use chaining for some simple tasks like:
作为先前答案的替代方案:有时将链接用于一些简单的任务可能更干净,例如:
uwv.parent('[data-update-with-value]')
.css({display: 'block'});