有人知道如何将文本框中的键盘插入符移动到特定位置吗?
例如,如果一个文本框(例如输入元素,而不是文本区域)中有50个字符,并且我想在插入字符20之前放置插入符号,那么我该如何处理呢?
这与这个问题不同:jQuery在文本区域设置光标位置,这需要jQuery.
Chrome中的输出:
<div id="content" contenteditable="true" style="border:1px solid #000;width:500px;height:40px;">
hey
<div>what's up?</div>
<div>
<button id="insert_caret"></button>
Run Code Online (Sandbox Code Playgroud)
我相信FF看起来像这样:
hey
<br />
what's up?
Run Code Online (Sandbox Code Playgroud)
在IE中:
hey
<p>what's up?</p>
Run Code Online (Sandbox Code Playgroud)
不幸的是,没有很好的方法来制作它,以便每个浏览器插入一个<br />而不是div或p-tag,或者至少我在网上找不到任何东西.
无论如何,我现在要做的是,当我按下按钮时,我希望将插入符号设置在文本的末尾,所以它应该看起来像这样:
hey
what's up?|
Run Code Online (Sandbox Code Playgroud)
任何方式这样做,所以它适用于所有浏览器?
例:
$(document).ready(function()
{
$('#insert_caret').click(function()
{
var ele = $('#content');
var length = ele.html().length;
ele.focus();
//set caret -> end pos
}
}
Run Code Online (Sandbox Code Playgroud) 我是网站开发的新手,并试图弄清楚如何在点击链接(使用html,php或javascript)时让我的用户自动将代码复制到他/她的鼠标(剪贴板)中.例如,我正在尝试创建这个个人网站,当用户点击我网站上的链接或按钮时,它应该自动将该文本代码复制到剪贴板.我看过像retailmenot.com这样的网站这样做:示例: -

如果可以,请告诉我一个例子
更新:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$("#link").click(function(){
var holdtext = $("#clipboard").innerText;
Copied = holdtext.createTextRange();
Copied.execCommand("Copy");
});
</script>
</head>
<body>
<hr>
<a href="http://www.w3schools.com" style="font-family:arial;color:black;font-size:25px;">Click here to copy the Code</a> <button onclick="copyToClipboard()">Copy Text</button>
<hr>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我正在尝试测试 React 组件,但似乎遇到了 JSDOM 问题。
当我安装我的组件时;
const component = mount(
<PipelineActions pipelineActions={value} {...actions} />
);
Run Code Online (Sandbox Code Playgroud)
我收到一个错误;
document.body.createTextRange 不是函数
我尝试直接通过设置dom;
(global as any).document = jsdom.jsdom('');
(global as any).window = document.defaultView;
Run Code Online (Sandbox Code Playgroud)
这没有效果。当我尝试时,console.log(document.body)我也会得到一些奇怪的东西;
HTMLBodyElement {}
似乎 DOM 没有正确构建,但我不确定为什么。有没有人见过这个?
我想在点击按钮上选择div内容.
HTML
<div id="divid">Hello This div content have to be select. </div>
<button onclick="selectText(divid);"> Select Div</button>
Run Code Online (Sandbox Code Playgroud)
JS
function selectText(divid) {
if (document.selection) {
var div = document.body.createTextRange();
div.moveToElementText(document.getElementById("divid"));
div.select();
}
else {
var div = document.createRange();
div.setStartBefore(document.getElementById("divid"));
div.setEndAfter(document.getElementById("divid"));
window.getSelection().addRange(div);
}
}
Run Code Online (Sandbox Code Playgroud)
javascript ×5
html ×3
caret ×1
enzyme ×1
input ×1
jestjs ×1
jsdom ×1
php ×1
textbox ×1
typescript ×1