我有一个简单的文本区域,我需要制作透明的字母,同时允许文本插入符号可见。当我应用以下规则时,就会得到不可见的插入符号:
textarea {
background: transparent;
opacity: 0;
}当我键入不可见文本时,我需要看到文本插入符号的移动。
编辑:我需要在表中编辑td单元格。当我单击一个单元格时,我会显示一个文本区域并开始键入。在每个字符的字母上,我在单元格中插入上下文。在那之后,我隐藏了一个文本区域。
发布于 2012-07-17 06:05:46
这个jsFiddle DEMO使用了一个在线教程方法,该方法经过了一些修改,以创建一个非本机 browser text-caret以及透明文本。
而且,我创建的这个jsFiddle New Method处理这个目标的方式不同,但对IE8并不友好。
状态更新:--我已经用这个名为“”的较新的版本改进了上述jsFiddle演示:
jsFiddle New Method that's Newer!!
上面的jsFiddle版本现在允许点击文本区域中的,插入符号将尊重单击的位置。这个额外的功能是由一个很好的问答here实现的。
发布于 2012-07-23 19:59:37
是时候把我的0.02美元扔进去了。
这是一个问题的答案,据我所知,这是有效的,它是快速和肮脏的,所以请随意提出建议。这段代码还没有经过测试,但是我在这里创建了一个工作小提琴:http://jsfiddle.net/66RXc/
<html>
<head>
<title>Testing</title>
<script type="text/javascript">
<!--
function call(val) {
document.getElementById('result').value += val.charAt(val.length - 1);
document.getElementById('result').value =
document.getElementById('result').value.substr(0, val.length);
document.getElementById('test').value =
document.getElementById('test').value.replace(/[^\^]/g, ' ');
}
-->
</script>
</head>
<body>
<textarea name="textarea" cols="20" rows="5" id="test"
onKeyUp="call(this.value);"></textarea>
<textarea style="display:block" cols="20" rows="5" id="result" disabled>
</textarea>
</body>
</html>每次在textarea "test“中键入一个字符,将其复制到一个隐藏的文本框中,并将"test”中的所有字符替换为空格时,我就会这样做。人物是隐藏的,克拉还在那里。全文仍在另一个方框中。您可以使用显示:隐藏而不是显示:块来隐藏它。
这并不是世界上最好的实现,只是我很快就做到了。你必须键入一种慢速(~15-20 WPM)才能让它工作。
发布于 2012-07-29 04:28:48
下面是一个使文本本身透明的CSS3解决方案:
将文本的颜色属性设置为color: rgba(0,0,0,0);
唯一的问题是插入符号看不见了。我做了一个快速搜索,发现插入符号和它的样式完全可以供浏览器使用。因此,我能想到的唯一选项就是使用Javascript在输入的末尾添加一个模拟的插入符号。
我对如何做到这一点有一个想法,但它很混乱,我不认为它是理想的-不过,我会写它,以防它有助于推进别人的想法:
display: none; (因此它有实际的宽度)white-space: nowrap;设置为将其保持在一行上)
<span id="caret">|</span> (我将将其称为规范其余部分的插入符号) position: relative;
- next you will have to call a function to compare the width of the label with the width of the textarea
- if the label is less wide than the textarea, get the width of the last span in the hidden label and shift the caret to the right by that width, then move on to step 4
- as this is function will be run as text is entered it will happen one character at a time
- be careful here that the caret doesn't go outside the textarea when it's in its last and near last positions
- if the label is wider than the textarea:
- add the widths of the characters (spans) in the label one at a time until you reach the width of the textarea
- shift the position of the caret down by the height of the font and back to the horizontal starting position (as the caret's position is relative, just change its left position back to `(0 + offsetToACTUALCaretPosition)`
- use a flag (e.g. `class="break"`) to mark the last span (character) in the previous row
- call the width comparison function again
- make sure that you include a condition to check for the flags that you added at the end of each "row" (if any)
color: rgba(0,0,0,0);。注意事项:
正如我之前说过的,我知道这个解决方案很粗糙,但它可能会帮助人们想出一个更好的解决方案。
祝好运!
https://stackoverflow.com/questions/11516736
复制相似问题