- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的目标是拥有一个输入元素,其中的占位符会在您将字符键入输入时逐字符消失。最好使用带有占位符 MM/DD/YYYY
的“生日”输入来解释这一点。如果您在输入中键入 06
,占位符 /DD/YYYY
仍应保留。
虽然这个问题的答案是: text box with placeholder text that dissapears character by character ,我还有一个限制:“输入”必须是“contenteditable="true"
”div
。接受的答案不适用于 contenteditable
div。
纯 HTML、CSS 和 JavaScript 的纯解决方案是最好的。请不要使用 jQuery。
最佳答案
这是我的技巧:
contenteditable
后面contenteditable
需要根据内容大小增长。这是通过设置 display: inline-block
来完成的在 contenteditable
上.contenteditable
应具有相同的背景颜色。onclick
函数被添加到父元素以允许关注 editablecontent
而它的宽度是0%monospace
字体用于确保字体中的每个字符具有相同的宽度(这样一来,您编写的每个字符都恰好隐藏一个字符)contenteditable
在宽度为 0 处,添加了一个假插入符号以指示焦点(这实际上在占位符内)。这是使用 blink
完成的动画,与一般兄弟组合器(~
),:empty
选择器和 :focus
选择器after
实现或 before
伪元素,但由于我们不能将伪元素放入 contenteditable
中,使用实际的 dom 元素作为占位符允许使用通用兄弟组合器来处理 contenteditable
空状态CSS 变量 用于同步主容器内嵌套的所有元素的颜色。我添加了版本:灰色背景和白色背景。此外,这是此处使用的唯一 CSS 功能,尚未被所有现代浏览器完全支持。所以如果你想要完整的浏览器支持,你可以专门放弃它,因为它对结果不是必需的:-)
IE & Edge 仍然在空白状态下显示原始插入符号。(我在 javascript 中添加了一个函数来删除这些浏览器上的假插入符)
// Get IE or Edge browser version
var isIEOrEdge = detectIE();
if (isIEOrEdge) {
let editables = document.querySelectorAll('.kb-editable');
for (let i = 0; i < editables.length; i++) {
editables[i].classList.add('kb-edge');
}
}
// this is the only function that is actually needed if the double IE\Edge caret doesn't bother you
function getFocus(id) {
document.getElementById(id).focus();
}
/* VERY OPTIONAL :-) */
/**
* detect IE
* returns version of IE or false, if browser is not Internet Explorer
*/
function detectIE() {
var ua = window.navigator.userAgent;
// Test values; Uncomment to check result …
// IE 10
// ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)';
// IE 11
// ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';
// Edge 12 (Spartan)
// ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0';
// Edge 13
// ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586';
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// Edge (IE 12+) => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}
// other browser
return false;
}
.kb-editable {
--bg-color: #D3D3D3;
--placeholder-color: grey;
--text-color: black;
--border-color: transparent;
--padding: 5px;
width: 200px;
height: 200px;
background: var(--bg-color);
position: relative;
align-items: start;
font-family: 'Anonymous Pro', monospace;
font-size: 14px;
overflow-y: auto;
display: inline-block;
cursor: text;
border: 1px solid var(--border-color);
padding: var(--padding);
}
.kb-editable [contenteditable="true"] {
position: relative;
z-index: 2;
background: var(--bg-color);
color: var(--text-color);
outline: none;
max-width: 100%;
max-height: 100%;
display: inline-block;
/* deal with long words (break them to multiple lines) */
word-wrap: break-word;
}
.kb-editable .kb-placeholder {
position: absolute;
top: var(--padding);
bottom: var(--padding);
left: var(--padding);
right: var(--padding);
color: var(--placeholder-color);
}
/* used for non-chrome to hide original caret on empty state */
[contenteditable="true"]:focus:empty {
color: transparent;
text-shadow: 0 0 0 black;
}
.kb-editable:not(.kb-edge) [contenteditable="true"]:focus:empty~.kb-placeholder:before {
content: "|";
color: var(--text-color);
position: absolute;
top: 0;
left: 0;
animation: 1s blink step-end infinite;
caret-color: transparent;
}
@keyframes blink {
from,
to {
color: transparent;
}
50% {
color: black;
}
}
<link href="https://fonts.googleapis.com/css?family=Anonymous+Pro" rel="stylesheet">
<div class="kb-editable" onclick="getFocus('black')">
<div contenteditable="true" id="black"></div>
<span class="kb-placeholder">I'm a placeholder</span>
</div>
<div class="kb-editable" onclick="getFocus('white')" style="--bg-color: white; --border-color: black">
<div contenteditable="true" id="white"></div>
<span class="kb-placeholder">I'm the white version!</span>
</div>
关于javascript - 逐字符消失的占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50071679/
我正在使用 python 加密一些文件,但我在逐 block 读取文件时遇到问题。 有时不会返回最后一个 block 的所有数据。 当文件长度为 307200 字节时,我没有问题。当它的长度为 279
我正在使用 WebRTC 将文件发送到连接的对等方,并且我正在以块的形式发送文件。但是,我无法弄清楚如何让对等方在文件逐块流入时保存/下载文件。 我在网上找到的所有例子都推荐做这样的事情: // se
我用 Tiled 做了一张 map 。它的每一 block 图 block 都尺寸为 32x32 像素,我的主要角色 Sprite 也是。 在我的类(class) Player.cpp 中,我有一些计
我见过一些单页网站,您可以逐 block 滚动,因此您没有无限滚动。 你逐 block 移动。 是否有提供此功能的任何脚本或其他东西? 最佳答案 我自己从未使用过它,所以我无法在代码方面为您提供帮助,
这是一个逐 block 反转文件内容的程序。 #include #include #define BS 12 void reverse(char * buffer, int size) { c
在下面的代码中,有没有办法避免 if 语句? s = 13; /*Total size*/ b = 5; /*Block size*/ x = 0; b1 = b; while(x s)
我正在尝试分割输入图像并逐个对其进行模糊处理,但毕竟对相邻图 block 调用 cv::blur 我得到了边界像素,这与我有一次将 cv::blur 集体应用于整个图像。 Mat upper(im,
我想逐个读取文件。该文件被分成几部分,存储在不同类型的媒体上。我目前所做的是调用文件的每个单独部分,然后将其合并回原始文件。 问题是我需要等到所有 block 都到达后才能播放/打开文件。是否可以在
我有一个包含客户和日期列表的 JSON 文件。 文件看起来像这样: { "Customers": [ { "Customer": "Customer Name Here", "Company"
我的邮件目标是从连接到HTTP服务器的TCP套接字读取数据,然后解析 HTTP响应块(传输编码:分块)-服务器在同一连接上每30秒发送一个块 我附上了我的代码。看起来io.Copy读取第一个块,然后等
我认为自己是一位经验丰富的 numpy 用户,但我无法找到以下问题的解决方案。假设有以下数组: # sorted array of times t = numpy.cumsum(numpy.rando
当我将文件添加到暂存区时,我可以 $ git add my_file -p 然后选择我要暂存的 block 。 有没有办法 merge/挑选一个提交并逐 block 应用它的差异? 谢谢 最佳答案 我
我有一个 mongodb 查询,它获取大约 50,000 个大文档。 这对我的 RAM 来说太多了,因此计算机速度变慢了。 现在我想逐 block 迭代 mongodb 结果。 我想获取前 1000
我不会为 AES 或其他加密打开此线程,因为这是我要用来加密 AES 和其他加密的 key 的内容。我从 StackOverflow 和其他一些网站收集了一些代码,并对其进行了编辑以适合我的程序,但是
我在做一些后台工作时尝试收集所有系统统计数据。例如,我使用以下命令来收集 IO 统计信息: iostat -xty 5 此脚本用于每 5 秒收集一次 I/O 统计信息。所以我的日志会包含很多数据 bl
我需要 php 脚本,用于从 url 到服务器的可恢复文件下载。它应该能够开始下载,然后在捕捉时(30 秒 - 5 分钟)恢复,依此类推,直到完成整个文件。 perl 中有类似的东西 http://c
是否有标准的 Linux 命令可用于逐 block 读取文件?例如,我有一个大小为 6kB 的文件。我想读取/打印第一个 1kB,然后是第二个 1kB ...似乎 cat/head/tail 在这种情
我正在处理大量文件,我想逐 block 处理这些文件,假设在每批处理中,我想分别处理每 50 个文件。 如何使用 Spark Structured Streaming 来实现? 我看到 Jacek L
我正在处理大量文件,我想逐 block 处理这些文件,假设在每批处理中,我想分别处理每 50 个文件。 如何使用 Spark Structured Streaming 来实现? 我看到 Jacek L
我想知道:逐 block 读取 jp2 并将数据存储在缓冲区对象中的预期方法是什么? 现在我正在做类似的事情。 /* note I already created stream and configu
我是一名优秀的程序员,十分优秀!