[英]How to define the css :focus state in a jQuery selector?
Here is a resizable textarea: 这是一个可调整大小的文本区域:
var KeyDown; $(".TxtArea > div").mousedown(function(){ $(this).parent().addClass("Resize"); $("body").addClass("UnSelectable"); KeyDown = 1; $("textarea").css("opacity","0.3"); $("textarea").focus(function() { $(this).css("border-color","#ccc") }); }); $(document).mouseup(function(){ $(".TxtArea").removeClass("Resize"); $("body").removeClass("UnSelectable"); KeyDown = 0; $("textarea").css("opacity","1"); $("textarea").focus(function() { $(this).css("border-color","#07c") }); }); $(document).mousemove(function(Event){ if (KeyDown == 1 && $(".TxtArea").hasClass("Resize")) { var Height = Event.pageY - $(".TxtArea").children("textarea").offset().top; $("textarea").height(Height); } });
textarea { border: 1px solid #ccc; outline:none; } textarea:focus{ border: 1px solid #07c; } .TxtArea { width: 300px; } .TxtArea > textarea { width: 100%; display: block; resize: none; box-sizing: border-box; } .TxtArea > div { height: 10px; background: #eee; border: 1px solid #ddd; box-sizing: border-box; text-align: center; line-height: 0px; } .TxtArea > div:hover { cursor: n-resize; } .UnSelectable { -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="TxtArea"> <textarea></textarea> <div>.....</div> </div>
Please follow these steps: 请按照以下步骤操作:
#ccc
and opacity is 0.3
) 移动鼠标并调整文本区域的大小(现在边框颜色为#ccc
,不透明度为0.3
) 1
but border doesn't change) 调整大小后,手指保持点击状态(不透明度现在为1
但边框未更改) Why border doesn't change? 为什么边界不变? and how can I return it to #07c
after unclicking. 以及#07c
后如何将其返回到#07c
07c。 How can I do that? 我怎样才能做到这一点? (I want something exactly like SO) (我想要完全像SO的东西)
Note: I want to set #07c
just only focus
state of that textarea after unclicking (Not a permanent border) 注意:我想将#07c
设置为仅在取消单击后该文本区域的focus
状态(不是永久性边框)
So I'm not sure exactly why your focus code wasn't working, but in general, this is just better practice. 因此,我不确定您的焦点代码为什么不起作用,但是总的来说,这是更好的做法。 Typically you want the css to handle all of the styling and the javascript just toggles it. 通常,您希望css处理所有样式,而javascript仅将其切换。 It just keeps things cleaner and more organized. 它只是使事情更清洁,更有条理。
So you can do this: https://jsfiddle.net/psp12a0n/ 因此,您可以执行以下操作: https : //jsfiddle.net/psp12a0n/
The main thing that was changed was this part in the javascript: 更改的主要内容是javascript中的这一部分:
$(".TxtArea > div").mousedown(function(){
$(this).parent().addClass("Resize");
$("body").addClass("UnSelectable");
KeyDown = 1;
$("textarea").addClass('inactive');
});
$(document).mouseup(function(){
$(".TxtArea").removeClass("Resize");
$("body").removeClass("UnSelectable");
KeyDown = 0;
$("textarea").removeClass('inactive');
});
And this in the css: 而在CSS:
textarea:focus,
textarea:active {
border: 1px solid #07c;
}
textarea.inactive {
opacity: .3;
border-color: #ccc;
}
Hope this works for you! 希望这对您有用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.