C# 在按键事件中对齐页面中间的文本框
我试图在文本框的按键事件页面中央对齐文本框,但我只能用下面给出的代码放大文本框的大小C# 在按键事件中对齐页面中间的文本框,c#,javascript,html,asp.net,textbox,C#,Javascript,Html,Asp.net,Textbox,我试图在文本框的按键事件页面中央对齐文本框,但我只能用下面给出的代码放大文本框的大小 <title></title> <script type="text/javascript"> function resize(selectObj) { selectObj.style.height = '400px'; selectObj.style.width = '800px'; selectObj.st
<title></title>
<script type="text/javascript">
function resize(selectObj) {
selectObj.style.height = '400px';
selectObj.style.width = '800px';
selectObj.style.padding = '10px 10px 10px 10px';
selectObj.style.position = 'absolute';
}
</script>
函数调整大小(选择对象){
选择obj.style.height='400px';
选择obj.style.width='800px';
选择obj.style.padding='10px 10px 10px 10px';
选择obj.style.position='absolute';
}
你可以这样做
$( "#txt" ).keypress(function() {
$("#maindiv").css({'display' : 'block', 'text-align' : 'center'});
});
屏幕中心的更新
$( "#txt" ).keypress(function() {
$(this).css('margin-left', ($(window).width() / 2));
});
您可以使用绝对定位将其居中。您应该考虑文本框的宽度和高度:
$( "#txt" ).keypress(function() {
$(this).css({
'position': 'absolute',
'left': ($(window).width() / 2 - $(this).width() / 2) + 'px',
'top': ($(window).height() / 2 - $(this).height() / 2) + 'px',
});
这对你肯定有帮助
$( "#txt" ).keypress(function() {
$(this).css({
'position': 'absolute',
'left': ($(window).width() / 2 - $(this).width() / 2) + 'px',
'top': ($(window).height() / 2 - $(this).height() / 2) + 'px',
});
您需要设置左边距和右边距属性以使其居中。我看不出OP已将问题标记为JQuery@Satpal将其更改为javascriptit工作正常,但我希望文本框位于keypress@RohitSingh屏幕中心或形状或屏幕中心@Rex
$( "#txt" ).keypress(function() {
$(this).css({
'position': 'absolute',
'left': ($(window).width() / 2 - $(this).width() / 2) + 'px',
'top': ($(window).height() / 2 - $(this).height() / 2) + 'px',
});
$( "#txt" ).keypress(function() {
$(this).css({
'position': 'absolute',
'left': ($(window).width() / 2 - $(this).width() / 2) + 'px',
'top': ($(window).height() / 2 - $(this).height() / 2) + 'px',
});