- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我发现 JavaScript 代码允许我在 textarea
字段中突出显示的文本周围添加 html 标签:
<textarea id="myArea" cols="30" spellcheck="false">Select some text within this field.</textarea>
<button onclick="ModifySelection ()">Modify the current selection</button>
<script>
function ModifySelection () {
var textarea = document.getElementById("myArea");
if('selectionStart' in textarea){
if (textarea.selectionStart != textarea.selectionEnd) {
var newText = textarea.value.substring (0, textarea.selectionStart) +
"[start]" + textarea.value.substring (textarea.selectionStart, textarea.selectionEnd) + "[end]" +
textarea.value.substring (textarea.selectionEnd);
textarea.value = newText;
}
}
}
</script>
我的问题与这一行有关,if('selectionStart' in textarea){
:
selectionStart
必须用引号引起来?selectionStart
通常附加到像这样的对象 "textarea"(textarea.selectionStart)
,如何引用到 它本身?If(X in Y){}
是标准 JavaScript 语法还是有效 专门用于selectionStart
?注意:我正在 jsfiddle 中测试它
最佳答案
if('selectionStart' in textarea)
是功能测试 - 它检查 textarea
对象是否具有 selectionStart
属性。
它需要用引号引起来,否则它将被解释为变量(当前上下文中可能存在也可能不存在)。
这取决于浏览器以及支持和呈现的 HTML 版本。
是的,这是正常的 JavaScript 语法。它用于测试指定的属性是否在对象中。请参阅in
在 MDN 上。
关于javascript - 将 html 标签添加到文本区域中选定的文本。为什么 "if(' SelectionStart' in textarea)"需要在那里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7619063/
这个问题在这里已经有了答案: selectionStart-End with textareas (2 个答案) 关闭 10 年前。 据我了解,selectionStart 必须从输入文本或文本区域
我正在尝试对文本框使用 SelectionStart 和 SelectionLength 属性。它没有效果,但也没有错误。它实际上是后台工作程序 ProgressChanged 方法的一部分,但我已经
我需要向用户显示,就像在 notepad.exe 中一样,他在光标上的文本的选择开始和长度。 选择长度没有问题,因为 Richtextbox 支持带有开始和结束的选择属性。 http://msdn.m
我正在为彩色文本使用 RichTextBox。假设我想为文本的不同部分使用不同的颜色。到目前为止一切正常。 我目前遇到 RichTextBox 的 SelectionStart 属性问题。我已经为 R
我在让下面的脚本与我的文本编辑器一起工作时遇到了一些困难。我不确定出了什么问题,但 selectionStart 和 selectionEnd 返回时未定义。 它应该在可编辑的 iframe 中获取突
在我的简单书签中,我调用了文档的所有 input 元素,然后尝试访问每个元素的 selectionStart: javascript: (function () { var inps=documen
我遇到了这个烦人的问题,我似乎无法在文本区域中获取所选文本的开始和结束索引,我得到的所有内容都是未定义的,如下所示: $('#myarea').selectionStart; // return un
我正在使用 selectionStart和 selectionEnd为了获得文本选择的起点和终点。 代码:https://codesandbox.io/s/busy-gareth-mr04o 但是,我
如果我输入 a然后 进入 IE11 文本区域并记录 selectionStart ,它是 2 (这是我所期望的)。但是,如果我以编程方式将 textarea 的值设置为 'a\n'相反,selecti
return layout.getLineForOffset(selectionStart); 这条线导致我的应用崩溃。 我正在用它来获取多行编辑文本中的当前光标行。像这样。 public int g
我正在尝试获取用户在文本框上单击的插入符号位置。我想在用户点击输入的位置添加一定的文本。我正在尝试在 input 的 onFocus 方法中执行此操作。但是,e.target.selectionSta
我正在尝试检测用户在关注 HTML 输入元素时点击的完整单词,然后查看它是否是 URL。我正在使用以下方法: let el = this.reminderInput.nativeElement var
每当我突出显示selectionStart = 0;时,该代码不执行。但是,如果我突出显示文本区域中第一个字符前面的一个字符,则代码将起作用。如果我突出显示文本区域中的第一个字符,有什么想法可以使此代
$(element)[0].selectionStart 似乎只适用于文本区域。是否有非文本区域的替代方案 我正在尝试将字符串包装在标记(粗体、斜体...)中的 DOM 元素中 另外,如何取消包装?
我有一个多行文本框,我想在文本框下方的标签中显示用户当前的行号和列号。看起来我可以使用 SelectionStart 属性、GetLineFromCharIndex 方法和 GetFirstCharI
我一直在努力使用 textarea 的 selectionStart 和 selectionEnd 属性,以使它们与 contenteditable div 元素一起使用。我在谷歌和SO上检查了很多相
我正在将一个 angularJS 文本插入器实现到一个文本区域,该文本区域将指定的文本插入到文本区域中光标的位置。经过一些谷歌搜索后,我在网上找到了这个示例。 指令的主体在这里 app.directi
使用这段代码,我可以检查 Firefox 中文本区域中的插入符位置: document.getElementById("myTextArea").selectionStart 这在 IE 8 中不起作
我有一个带有自定义样式的 TextBox 输入字段: 我添加了自定义样式:
如何根据鼠标位置设置 WinForms TextBox 的 SelectionStart?我正在扩展 TextBox 并希望在用户右键单击 TextBox 时将 carot 设置在鼠标点下方的位置(即
我是一名优秀的程序员,十分优秀!