Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在按键事件中对齐页面中间的文本框_C#_Javascript_Html_Asp.net_Textbox - Fatal编程技术网

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',
    });