简体   繁体   English

CSS如何使文本字段内的文本无法选择(直到第一个字符输入)并具有5px的左边距

[英]CSS How Do I Make the Text Inside Of a Textfield Unselectable (until first character entered) and Have A Left Margin Of 5px

I want to replicate the sign up form the way google has it: https://accounts.google.com/SignUp?continue=http%3A%2F%2Fwww.google.ca%2F&hl=en But I do not know how to turn the text inside the field unselectable. 我想以谷歌的方式复制注册表单: https//accounts.google.com/SignUp?continue = http% 3A%2F%2Fwww.google.ca%2F &hl =但我不知道怎么做翻译字段内的文字无法选择。 Here is the rub: I have to make textfield editable so I can't use readonly. 这是擦:我必须使文本域可编辑,所以我不能使用readonly。 See the link please if you need clarification. 如果您需要澄清,请查看链接。 Also, I don't know how to give the text inside the field margins 另外,我不知道如何在字段边缘内提供文本

Here is what I have so far: HTML: 这是我到目前为止:HTML:

<input type="text" onkeyup="validateName(this)" id="first" name="firstName" class="nameSignup" value="First">

CSS: #first { color: grey; CSS:#first {color:grey; } }

Javascript: 使用Javascript:

function validateName(id){
var str = id.value;

if(str == ""){
    id.value = "First";
    $(id).prop('selectionStart', 0)
                 .prop('selectionEnd', 0);
            //psudo code
             turn text grey
             turn text unselectable
             margin-left: 10px;
}else{
        //Pseudo code
        erase the default value
        turn text black
        turn text editable
        margin-left: 0px;
        check if the character is valid (not @, #, $, etc)
}
}

You can use HTML5 placeholder attribute. 您可以使用HTML5占位符属性。 This is not supported in older browsers though. 但是在旧版浏览器中不支持此功能。

<input type="text" onkeyup="validateName(this)" id="first" name="firstName" class="nameSignup"  placeholder="First">

Support For crossBrowser compatibilty probably you can check this 支持对于crossBrowser兼容性,您可以检查一下

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将元素从margin-left:-9999px移动到页面时,如何平滑过渡? - How do I make a smooth transition when moving an element from margin-left:-9999px to the page? CSS / JS:除div内的内容外,如何告诉我的页面完全无法选择? - CSS / JS: How do I tell my page to be completely unselectable, except for stuff inside a div? 如何在chrome中打印pdf但使pdf中的文本无法选择? - How to print pdf in chrome but make text inside pdf unselectable? 如果margin-left = 0px,如何更改margin-left-increase函数以使其禁用? - How can I alter my margin-left-increase function to disable if margin-left = 0px? 使用 JavaScript 如何获取填充/边距设置以始终检索 CSS 设置 % 而不是 px 大小 - Using JavaScript how do I get the padding/margin settings to always retrieve the CSS set % and not px size 如何使输入的文本和复选框文本转到同一个文本字段? - How can I make the entered text and checkbox text go to the same textfield? jQuery:如何:显示UL时,首先让LI动画“左” 20px,然后变为0px,然后让第二个LI动画,然后第三个,依此类推 - Jquery: how to: when UL is shown, have first LI animate 'left' 20px, then to 0px, then have the second LI do that, then third, etc 如何从字符串的左边删除字符,直到找到第一个字符: - How to remove character from left of the string until first : if found? 使SVG文本无法选择 - Make SVG Text unselectable 如何将输入到文本字段的文本格式化为货币? - How can I format the text entered into an HTML textfield like currency?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM